如何在存储过程中将查询值设置为输出变量

时间:2017-01-01 08:50:02

标签: sql-server join stored-procedures

这是我的SQL查询,当我执行它时,发生下面显示的错误。我是SQL Server的新手,任何人都可以解释这个错误的原因是什么?如何将此SQL JOIN查询设置为变量?

ALTER PROC spGetFullLog
    @PatientID varchar(10),
    @PatientD nvarchar(255) output
WITH ENCRYPTION
AS
BEGIN
    SELECT 
        @PatientD = First_Name,
        Last_Name,
        dbo.CalculateAge(Date_of_Birth) AS Age,
        Admitted_Date, Patient_Condition,
        Medicine_Prescribed, Treatment_Advice,
        Treatments_Given, Date_of_Discharged,
        Diagnosis, Treatments,
        Date_of_operation, Typpe_of_operation,
        Condition_Before, Condition_After
    FROM 
        Patients_Main
    FULL JOIN
        Admitted_Patients ON Patients_Main.Patient_ID = Admitted_Patients.Patient_ID
    LEFT JOIN
        Discharged_Patients ON Patients_Main.Patient_ID = Discharged_Patients.Patient_ID
    LEFT JOIN 
        Patient_Consultation_Details ON Patients_Main.Patient_ID = Patient_Consultation_Details.Patient_ID
    LEFT JOIN 
        Operation ON Patients_Main.Patient_ID = Operation.Patient_ID
    LEFT JOIN
        Patient_Conditon ON Patients_Main.Patient_ID = Patient_Conditon.Patient_ID
    WHERE
        Patients_Main.Patient_ID = @PatientID
END

这是我用来执行此存储过程的代码

DECLARE @Details nvarchar(255)
EXECUTE spGetFullLog 'PT0001', @Details output
print @Details

这是错误

  

消息141,级别15,状态1,过程spGetFullLog,第7行
  为变量赋值的SELECT语句不能与数据检索操作结合使用。

3 个答案:

答案 0 :(得分:0)

您不仅可以将部分列分配给变量。你应该单独做。

ALTER PROC spGetFullLog
    @PatientID varchar(10),
    @PatientD nvarchar(255) output
WITH ENCRYPTION
AS
BEGIN
    SELECT 
         @PatientD = First_Name
    FROM  Patients_Main
    WHERE  Patients_Main.Patient_ID = @PatientID


     SELECT 
    @PatientD = First_Name
    Last_Name,
    dbo.CalculateAge(Date_of_Birth) AS Age,
    Admitted_Date, Patient_Condition,
    Medicine_Prescribed, Treatment_Advice,
    Treatments_Given, Date_of_Discharged,
    Diagnosis, Treatments,
    Date_of_operation, Typpe_of_operation,
    Condition_Before, Condition_After
FROM 
    Patients_Main
FULL JOIN
    Admitted_Patients ON Patients_Main.Patient_ID = Admitted_Patients.Patient_ID
LEFT JOIN
    Discharged_Patients ON Patients_Main.Patient_ID = Discharged_Patients.Patient_ID
LEFT JOIN 
    Patient_Consultation_Details ON Patients_Main.Patient_ID = Patient_Consultation_Details.Patient_ID
LEFT JOIN 
    Operation ON Patients_Main.Patient_ID = Operation.Patient_ID
LEFT JOIN
    Patient_Conditon ON Patients_Main.Patient_ID = Patient_Conditon.Patient_ID
WHERE
    Patients_Main.Patient_ID = @PatientID
END

答案 1 :(得分:0)

您得到的错误非常清楚地解释了您的错误 - 您无法在一个查询中设置变量值并检索值。你必须使用2个查询来获得你想要的结果:

ALTER PROC spGetFullLog
    @PatientID varchar(10),
    @PatientD nvarchar(255) output
WITH ENCRYPTION
AS
BEGIN

    SELECT 
        @PatientD = First_Name
    FROM 
        Patients_Main
    WHERE
        Patient_ID = @PatientID;

    SELECT 
        Last_Name,
        dbo.CalculateAge(Date_of_Birth) AS Age,
        Admitted_Date, Patient_Condition,
        Medicine_Prescribed, Treatment_Advice,
        Treatments_Given, Date_of_Discharged,
        Diagnosis, Treatments,
        Date_of_operation, Typpe_of_operation,
        Condition_Before, Condition_After
    FROM 
        Patients_Main
    FULL JOIN
        Admitted_Patients ON Patients_Main.Patient_ID = Admitted_Patients.Patient_ID
    LEFT JOIN
        Discharged_Patients ON Patients_Main.Patient_ID = Discharged_Patients.Patient_ID
    LEFT JOIN 
        Patient_Consultation_Details ON Patients_Main.Patient_ID = Patient_Consultation_Details.Patient_ID
    LEFT JOIN 
        Operation ON Patients_Main.Patient_ID = Operation.Patient_ID
    LEFT JOIN
        Patient_Conditon ON Patients_Main.Patient_ID = Patient_Conditon.Patient_ID
    WHERE
        Patients_Main.Patient_ID = @PatientID
END

答案 2 :(得分:0)

我认为值得向你解释为什么你所做的事情毫无意义。

您正在尝试为变量@PatientD分配值。 [假设语句在语法上是合法的,而不是]要使用的值是过滤数据的结果,因此只有在过滤过程完成后,才会进行赋值。因此,使用相同的变量作为过滤器的一部分将意味着变量在过滤过程中没有分配任何值。那种 CATCH 22 的东西。

希望这能使你的事情更加清晰。