Можно ли динамически получать значение параметра из базы данных в хранимых процедурах MSSQL? - PullRequest
0 голосов
/ 28 декабря 2018

Я хочу отобразить все свойства автомобиля при сканировании его штрих-кода с помощью сканера штрих-кода.Ввод каждого штрих-кода каждого транспортного средства хранится в базе данных Microsoft SQL.Можно ли написать процедуру по этому поводу?Если да, то дайте мне несколько идей.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[vq_proc]
@Vehicle_id varchar(50),
@L1_Employee varchar(50) output,
@L2_Employee varchar(50) output,
@L3_Employee varchar(50) output,
@L4_Employee varchar(50) output,
@R1_Employee varchar(50) output,
@R2_Employee varchar(50) output,
@R3_Employee varchar(50) output,
@AF_Date varchar(50) output,
@AF_Time varchar(50) output,
@LWR varchar(50) output,
@Noise varchar(50) output,
@LWL varchar(50) output,
@Elec varchar(50) output

AS
BEGIN

  --L1 employee
  Select @L1_Employee= emp.Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='L1' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --L2
  Select @L2_Employee= emp.Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='L2' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --L3
  Select @L3_Employee= emp.Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='L3' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --L4
  Select @L4_Employee= emp.Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='L4' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id

  --R1
  select @R1_Employee= emp.Employee_name from log_Station s1,HVQ_Employee emp where s1.station_code='R1' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --R2
   select @R2_Employee= emp.Employee_name from log_Station s1,HVQ_Employee emp where s1.station_code='R2' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --R3
  Select @R3_Employee= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='R3' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id

  --Aq
  Select @AF_Date= CONVERT(VARCHAR(10),timestamp,101),@AF_Time= CONVERT(VARCHAR(10),timestamp,108)/*, */ FROM log_Station s1 where  @Vehicle_id= s1.Vehicle_id/*HVQ_Vehicle*/ 
  --LW &R
  Select @LWR= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='LW' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --Noise
  Select @Noise= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='Noise' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --LW & L
  Select @LWL= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='LW' and s1.Employee_id=emp.Employee_id  and s1.Vehicle_id= @Vehicle_id
  --Elc
  Select @Elec= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='Elc' and s1.Employee_id=emp.Employee_id  and s1.Vehicle_id= @Vehicle_id


SELECT  @L1_Employee as N'@L1_Employee',
        @L2_Employee as N'@L2_Employee',
        @L3_Employee as N'@L3_Employee',
        @L4_Employee as N'@L4_Employee',
        @R1_Employee as N'@R1_Employee',
        @R2_Employee as N'@R2_Employee',
        @R3_Employee as N'@R3_Employee',
        @AF_Date as N'@AF_Date',
        @AF_Time as N'@AF_Time',
        @LWR as N'@LWR',
        @Noise as N'@Noise',
        @LWL as N'@LWL',
        @Elec as N'@Elec'

END

Когда я сканировал штрих-код, идентификатор автомобиля должен отображаться в поле номера кадра.И мне нужно написать хранимую процедуру, чтобы вытащить идентификатор автомобиля из базы данных.This is what I am trying to write the procedure for

1 Ответ

0 голосов
/ 28 декабря 2018

Мне не понятны ваши данные, но я подготовил вашу хранимую процедуру.Все выбранные вами строки выбирают имя сотрудника, если это неверно, вы можете настроить то, что здесь делается:

Таблицы:

Create Table log_station
(
Employee_id varchar(10),
Vehicle_id varchar(50),
station_code varchar(10)

)
Insert Into log_station Values
('x','1','L1'),
('x','1','L2'),
('x','1','L3'),
('x','1','L4'),
('y','1','R1'),
('y','1','R2'),
('y','1','R3'),
('x','1','LWR'),
('x','1','Noise'),
('x','1','LWL'),
('x','1','Elec')

Create Table HVQ_Employee
(
Employee_id varchar(10),
employee_name varchar(50)
)
Insert Into  HVQ_Employee Values
('x','Bill Smith'),
('y','Mary Jones')

Хранимая процедура: некоторые изменения были внесены в вашproc для GetDate и несколько критериев выбора, как представляется, имеют неправильное написание

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter  PROCEDURE [dbo].[vq_proc]
@Vehicle_id varchar(50),
@L1_Employee varchar(50) output,
@L2_Employee varchar(50) output,
@L3_Employee varchar(50) output,
@L4_Employee varchar(50) output,
@R1_Employee varchar(50) output,
@R2_Employee varchar(50) output,
@R3_Employee varchar(50) output,
@AF_Date varchar(50) output,
@AF_Time varchar(50) output,
@LWR varchar(50) output,
@Noise varchar(50) output,
@LWL varchar(50) output,
@Elec varchar(50) output

AS
BEGIN

  --L1 employee
  Select @L1_Employee= emp.Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='L1' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --L2
  Select @L2_Employee= emp.Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='L2' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --L3
  Select @L3_Employee= emp.Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='L3' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --L4
  Select @L4_Employee= emp.Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='L4' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id

  --R1
  select @R1_Employee= emp.Employee_name from log_Station s1,HVQ_Employee emp where s1.station_code='R1' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --R2
   select @R2_Employee= emp.Employee_name from log_Station s1,HVQ_Employee emp where s1.station_code='R2' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --R3
  Select @R3_Employee= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='R3' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id

  --Aq
  Select @AF_Date= CONVERT(VARCHAR(10),GetDate(),101),@AF_Time= CONVERT(VARCHAR(10),GetDate(),108)/*, */ FROM log_Station s1 where  @Vehicle_id= s1.Vehicle_id/*HVQ_Vehicle*/ 
  --LW &R
  Select @LWR= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='LWR' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --Noise
  Select @Noise= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='Noise' and s1.Employee_id=emp.Employee_id and s1.Vehicle_id= @Vehicle_id
  --LW & L
  Select @LWL= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='LWL' and s1.Employee_id=emp.Employee_id  and s1.Vehicle_id= @Vehicle_id
  --Elc
  Select @Elec= Employee_name From log_Station s1,HVQ_Employee emp where s1.station_code='Elec' and s1.Employee_id=emp.Employee_id  and s1.Vehicle_id= @Vehicle_id

/*
SELECT  @L1_Employee as N'@L1_Employee',
        @L2_Employee as N'@L2_Employee',
        @L3_Employee as N'@L3_Employee',
        @L4_Employee as N'@L4_Employee',
        @R1_Employee as N'@R1_Employee',
        @R2_Employee as N'@R2_Employee',
        @R3_Employee as N'@R3_Employee',
        @AF_Date as N'@AF_Date',
        @AF_Time as N'@AF_Time',
        @LWR as N'@LWR',
        @Noise as N'@Noise',
        @LWL as N'@LWL',
        @Elec as N'@Elec'
*/
END

Выполнить:

Declare @Vehicle_id varchar(50)
Declare @L1_Employee varchar(50)
Declare @L2_Employee varchar(50)
Declare @L3_Employee varchar(50)
Declare @L4_Employee varchar(50)
Declare @R1_Employee varchar(50)
Declare @R2_Employee varchar(50)
Declare @R3_Employee varchar(50)
Declare @AF_Date varchar(50)
Declare @AF_Time varchar(50)
Declare @LWR varchar(50)
Declare @Noise varchar(50)
Declare @LWL varchar(50) 
Declare @Elec varchar(50)

Set @Vehicle_id = '1'

Exec vq_proc @vehicle_id,
@L1_Employee Output,
@L2_Employee Output, 
@L3_Employee Output, 
@L4_Employee Output, 
@R1_Employee Output, 
@R2_Employee Output, 
@R3_Employee Output,
@AF_Date Output,
@AF_Time Output, 
@LWR Output, 
@Noise Output, 
@LWL Output, 
@Elec Output 

SELECT  @vehicle_id As N'@vehicle_id',
        @L1_Employee as N'@L1_Employee',
        @L2_Employee as N'@L2_Employee',
        @L3_Employee as N'@L3_Employee',
        @L4_Employee as N'@L4_Employee',
        @R1_Employee as N'@R1_Employee',
        @R2_Employee as N'@R2_Employee',
        @R3_Employee as N'@R3_Employee',
        @AF_Date as N'@AF_Date',
        @AF_Time as N'@AF_Time',
        @LWR as N'@LWR',
        @Noise as N'@Noise',
        @LWL as N'@LWL',
        @Elec as N'@Elec'

Результат:

@vehicle_id @L1_Employee    @L2_Employee    @L3_Employee    @L4_Employee    @R1_Employee    @R2_Employee    @R3_Employee    @AF_Date    @AF_Time    @LWR        @Noise      @LWL        @Elec
1           Bill Smith      Bill Smith      Bill Smith      Bill Smith      Mary Jones      Mary Jones      Mary Jones      12/28/2018  08:35:36    Bill Smith  Bill Smith  Bill Smith  Bill Smith
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...