如何将使用游标的代码转换为单个select语句?
ALTER PROC [dbo].[ItemComboWarningItem1]
@patientApptID int,
@iwcID int OUTPUT
AS
SET NOCOUNT ON
DECLARE @ItemComboWarningid int;
DECLARE warning_cursor CURSOR FOR
SELECT distinct [ItemComboWarningID]
FROM [dbo].[ItemComboWarning] [icw]
INNER JOIN [dbo].[ItemComboWarningItem][icwi] ON [icw].[ID]=[icwi].[ItemComboWarningID]
INNER JOIN [dbo].[PatientItem][pi] on [pi].[ItemID]=[icwi].[itemID]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
WHERE [pa].[id]=@patientApptID
OPEN warning_cursor
FETCH NEXT FROM warning_cursor
INTO @ItemComboWarningid
WHILE @@FETCH_STATUS = 0
BEGIN
(SELECT [icwi].[itemid]
FROM [dbo].[ItemComboWarning] [icw]
INNER JOIN [dbo].[ItemComboWarningItem][icwi] ON [icw].[ID]=[icwi].[ItemComboWarningID]
INNER JOIN [dbo].[PatientItem][pi] on [pi].[ItemID]=[icwi].[itemID]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
WHERE [pa].[id]=@patientApptID
AND [icwi].[ItemComboWarningid]=@ItemComboWarningid)
EXCEPT
(SELECT DISTINCT [pi].[itemID]
FROM [dbo].[PatientItem][pi]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
INNER JOIN [dbo].[ItemComboWarningItem][icwi] ON [pi].[ItemID]=[icwi].[itemID]
WHERE [pa].[id]=@patientApptID)
UNION
( SELECT DISTINCT [pi].[itemID]
FROM [dbo].[PatientItem][pi]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
INNER JOIN [dbo].[ItemComboWarningItem][icwi] ON [pi].[ItemID]=[icwi].[itemID]
WHERE [pa].[id]=@patientApptID)
EXCEPT
(SELECT [icwi].[itemid]
FROM [dbo].[ItemComboWarning] [icw]
INNER JOIN [dbo].[ItemComboWarningItem][icwi] ON [icw].[ID]=[icwi].[ItemComboWarningID]
INNER JOIN [dbo].[PatientItem][pi] ON [pi].[ItemID]=[icwi].[itemID]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
WHERE [pa].[id]=@patientApptID
AND [icwi].[ItemComboWarningid]=@ItemComboWarningid)
IF @@ROWCOUNT = 0
BEGIN
SET @iwcID = @ItemComboWarningid;
END
FETCH NEXT FROM warning_cursor INTO @ItemComboWarningid
END
CLOSE warning_cursor;
DEALLOCATE warning_cursor;
RETURN @iwcID
我尝试的是
Select [icw].[id], [icw].[Message]
FROM [dbo].[ItemComboWarning] [icw]
WHERE NOT EXISTS ( Select * from (SELECT [icwi].[itemid]
FROM [dbo].[ItemComboWarning] [icw2]
INNER JOIN [dbo].[ItemComboWarningItem][icwi] ON [icw2].[ID]=[icwi].[ItemComboWarningID]
INNER JOIN [dbo].[PatientItem][pi] on [pi].[ItemID]=[icwi].[itemID]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
WHERE [pa].[id]=3712182
AND [icwi].[ItemComboWarningid]=[icw].[id]
EXCEPT
(SELECT DISTINCT [pi].[itemID]
FROM [dbo].[PatientItem][pi]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
INNER JOIN [dbo].[ItemComboWarningItem][icwi2] ON [pi].[ItemID]=[icwi2].[itemID]
WHERE [pa].[id]=3712182)
UNION
( SELECT DISTINCT [pi].[itemID]
FROM [dbo].[PatientItem][pi]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
INNER JOIN [dbo].[ItemComboWarningItem][icwi3] ON [pi].[ItemID]=[icwi3].[itemID]
WHERE [pa].[id]=3712182)
EXCEPT
(SELECT [icwi4].[itemid]
FROM [dbo].[ItemComboWarning] [icw4]
INNER JOIN [dbo].[ItemComboWarningItem][icwi4] ON [icw4].[ID]=[icwi4].[ItemComboWarningID]
INNER JOIN [dbo].[PatientItem][pi] ON [pi].[ItemID]=[icwi4].[itemID]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
WHERE [pa].[id]=3712182
AND [icwi4].[ItemComboWarningid]=[icw].[id])))
但我收到此错误"期待AS,ID或Quoted_ID"。
主要想法是我想检查每个[icw]。[id]并且如果此查询没有返回任何行
SELECT [icwi].[itemid]
FROM [dbo].[ItemComboWarning] [icw2]
INNER JOIN [dbo].[ItemComboWarningItem][icwi] ON [icw2].[ID]=[icwi].[ItemComboWarningID]
INNER JOIN [dbo].[PatientItem][pi] on [pi].[ItemID]=[icwi].[itemID]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
WHERE [pa].[id]=3712182
AND [icwi].[ItemComboWarningid]=**[icw].[id]**
EXCEPT
(SELECT DISTINCT [pi].[itemID]
FROM [dbo].[PatientItem][pi]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
INNER JOIN [dbo].[ItemComboWarningItem][icwi2] ON [pi].[ItemID]=[icwi2].[itemID]
WHERE [pa].[id]=3712182)
UNION
( SELECT DISTINCT [pi].[itemID]
FROM [dbo].[PatientItem][pi]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
INNER JOIN [dbo].[ItemComboWarningItem][icwi3] ON [pi].[ItemID]=[icwi3].[itemID]
WHERE [pa].[id]=3712182)
EXCEPT
(SELECT [icwi4].[itemid]
FROM [dbo].[ItemComboWarning] [icw4]
INNER JOIN [dbo].[ItemComboWarningItem][icwi4] ON [icw4].[ID]=[icwi4].[ItemComboWarningID]
INNER JOIN [dbo].[PatientItem][pi] ON [pi].[ItemID]=[icwi4].[itemID]
INNER JOIN [dbo].[PatientAppointment][pa] ON [pa].[ID]=[pi].PatientAppointmentID
WHERE [pa].[id]=3712182
AND [icwi4].[ItemComboWarningid]=**[icw].[id]**)
然后我要显示该ID。但我需要从外表中与 [icw]。[id] 进行链接。怎么做到呢?谢谢!