【发布时间】:2017-01-01 08:50:02
【问题描述】:
这是我的 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 语句不得与数据检索操作结合使用。
【问题讨论】:
-
尝试再次运行它没有
PatientD =。我的猜测是您不会收到错误消息。如果我是正确的,这意味着您要么为所有提取的字段提供变量,要么不提供。如果您需要返回包含所有字段的记录集的过程,您可以简单地将所有值放入局部变量中,做任何您必须做的事情(使用变量PatientD)然后隐含SELECT返回前的所有变量。 -
@Spiderman:复杂而棘手??你是认真的吗?
-
另外一个观察:您将条件设置为
WHERE Patients_Main.Patient_ID = @PatientID。试试WHERE Patients_Main.Patient_ID = First_Name(去掉开头的赋值后)。
标签: sql-server join stored-procedures