Sql server 如何在存储过程中将查询值设置为输出变量
这是我的SQL查询,当我执行它时,下面显示的错误正在发生。我是SQL Server新手,有人能解释一下这个错误的原因吗?如何将此SQL联接查询设置为变量Sql server 如何在存储过程中将查询值设置为输出变量,sql-server,join,stored-procedures,Sql Server,Join,Stored Procedures,这是我的SQL查询,当我执行它时,下面显示的错误正在发生。我是SQL Server新手,有人能解释一下这个错误的原因吗?如何将此SQL联接查询设置为变量 ALTER PROC spGetFullLog @PatientID varchar(10), @PatientD nvarchar(255) output WITH ENCRYPTION AS BEGIN SELECT @PatientD = First_Name, Last_Name
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
这就是错误所在
Msg 141,第15级,状态1,程序spGetFullLog,第7行为变量赋值的SELECT语句不得与数据检索操作结合使用
您不仅可以将部分列指定给变量。你应该分开做
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
您得到的错误非常清楚地解释了您做错了什么-您不能在一个查询中设置变量值和检索值。您必须使用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
我认为有必要向你解释为什么你所做的毫无意义 您正试图为变量
@PatientD
赋值。[假设该语句在语法上是合法的,这是不合法的]要使用的值是筛选数据的结果,因此只有在筛选过程完成后,才会进行赋值。因此,使用同一变量作为过滤器的一部分将意味着该变量在过滤过程中没有分配任何值。有点像抓22个东西
希望这能让你更清楚。尝试在没有
PatientD=
的情况下再次运行。我猜您不会收到错误消息。如果我是正确的,这意味着要么为所有提取的字段提供变量,要么不提供变量。如果您需要该过程返回包含所有字段的记录集,只需将所有值转换为局部变量,执行您必须执行的操作(使用变量PatientD
),然后在返回之前暗示选择所有变量。@Spiderman:复杂而棘手??你是认真的吗?还有一个观察结果:你正在将条件设置为WHERE Patients\u Main.Patient\u ID=@PatientID
。尝试,其中Patients\u Main.Patient\u ID=First\u Name
(在开始删除分配后)。不太可能。有两个变量-PatientID和PatientD。第一个用于过滤查询,第二个用于输出结果。@ZoharPeled,OOOPPPSS!!!!!嗯,有人需要解释一些关于变量命名的事情。。。谢谢您的更正。@Nolan我想这段代码仍然会出现这样的错误,请再次检查这一行:)PatientD=First\u Name Last\u Name,您的意思是@PatientD应该是First Name concat Last Name?然后将是:@PatientD=ISNULL(FirstName+'',)+ISNULL(LastName'')