SQL Server:参数化存储过程

时间:2017-10-14 19:05:53

标签: sql-server stored-procedures

我必须在SQL Server中执行此操作。假设我有2个表。

根据参数NameSurname,我必须从PhysicianID获取Table1

之后,我必须使用insert into storage procedure创建新记录。

像这样的东西

CREATE PROCEDURE FIND_PHYSICIANID
     @FirstName varchar(50),
     @LastName varchar(50)
AS
BEGIN
    DECLARE  @PhysicianID  int

    SELECT @PhysicianID = PhysicianID 
    FROM Table1
    WHERE FirstName = @FirstName  AND LastName = @LastName

    RETURN @PhysicianID
END

EXECUTE FIND_PHYSICIANID 'Kathlin','Jones' 

CREATE PROCEDURE ADD_APPOINTMENT     -- Create a new appointment    
    @AppointmentType VARCHAR(70), --Type of new appointment
    @pAppointmentDate DATE, -- Date of new appointment
    @aPhysicianID INT, --PhysicianID of requested physician (in this case during execution we will take value which we know-read from table for requested first and last name)     
    @apPatientID INT, --PatientID of chosen patient(let's say any from 1 to 14) 
    @aScheduleID INT, --ScheduleID, but here we have to take some ScheduleID for chosen PhysicianID (in this case during execution we will take value which we know-based on PHYSICIANID we may read value from table SCHEDULE)  
    @Status CHAR(1) -- Just Y or N
AS    -- This "AS" is required by the syntax of stored procedures. 
BEGIN   -- Insert the new appointment  
    INSERT INTO [APPOINTMENT]([AppointmentType], [AppointmentDate],[aPhysicianID],
                              [apPatientID], [aScheduleID], [Status-Canceled])   
    VALUES (@AppointmentType, @pAppointmentDate, @aPhysicianID, 
            @apPatientID, @aScheduleID, @Status); 
END; 

EXECUTE ADD_APPOINTMENT 'Vaccinations', '2017-0831', '@PhysicianID', '12', '289', 'N'

2 个答案:

答案 0 :(得分:1)

你可以获得这样的返回ID。

DECLARE  @PhysicianID  int
EXECUTE @PhysicianID  = FIND_PHYSICIANID 'Kathlin','Jones' 

你可以像这样使用这个参数

EXECUTE ADD_APPOINTMENT 'Vaccinations','2017-0831', @PhysicianID, '12','289','N'

答案 1 :(得分:0)

假设找到医生的能力是一种常见操作,您可以将FIND_PHYSICIANID存储过程转换为函数,并将查找延迟到执行操作的消费存储过程中。

create function [dbo].[FIND_PHYSICIANID]
(   
     @FirstName varchar(50),
     @LastName varchar(50)
)
returns int
as
begin

    declare @PhysicianId int

    select @PhysicianID = PhysicianID 
    from dbo.Table1
    where FirstName = @FirstName  
        and LastName = @LastName

    return @PhysicianId

end

这仍将保持寻找医生集中的逻辑,但如果您可获得的唯一信息是全名,则允许您执行其他操作并可能进行验证。是的,它是更多的参数,但这是假设存储过程所需的参数是可管理的数量。

create procedure [dbo].[ADD_APPOINTMENT]     -- Create a new appointment    
    @AppointmentType VARCHAR(70), --Type of new appointment
    @pAppointmentDate DATE, -- Date of new appointment
    @PhysicianFirstName varchar(50), -- // The first name of the physician
    @PhysicianLastName varchar(50),  -- // The last name of the physician   
    @apPatientID INT, --PatientID of chosen patient(let's say any from 1 to 14) 
    @aScheduleID INT, --ScheduleID, but here we have to take some ScheduleID for chosen PhysicianID (in this case during execution we will take value which we know-based on PHYSICIANID we may read value from table SCHEDULE)  
    @Status CHAR(1) -- Just Y or N
AS    -- This "AS" is required by the syntax of stored procedures. 
BEGIN   -- Insert the new appointment  

    declare @aPhysicianID int
    select @aPhysicianID = [dbo].[FIND_PHYSICIANID](@PhysicianFirstName, @PhysicianLastName varchar(50))

    INSERT INTO [APPOINTMENT]([AppointmentType], [AppointmentDate],[aPhysicianID],
                              [apPatientID], [aScheduleID], [Status-Canceled])   
    VALUES (@AppointmentType, @pAppointmentDate, @aPhysicianID, 
            @apPatientID, @aScheduleID, @Status); 
END

或者,如果希望将其分开并保留现有的存储过程参数签名,那么让调用者通过本地存储过程查找医师id的先前答案,然后将该参数传递给添加约会存储过程应该足够你的要求。

根据下面的伪代码

  1. 为int
  2. 类型的@physicianid声明变量
  3. 将@physicianid变量分配给FIND_PHYSICIANID存储过程的输出
  4. 使用@physicianid变量作为输入执行ADD_APPOINTMENT存储过程