Update語句我有兩個表:問題在T-SQL
CREATE TABLE [dbo].[Task](
[SysTask] [int] IDENTITY(1,1) NOT NULL,
[TaskStatus] [int] NOT NULL,
)
CREATE TABLE [dbo].[Queue](
[SysQueue] [int] IDENTITY(1,1) NOT NULL,
[SysTask] [int] NOT NULL,
[QueueStatus] [int] NOT NULL,
)
一個任務有很多隊列記錄。我知道這些名字有點混亂。 我需要更新表TASK中的所有記錄,這些記錄在表QUEUE中沒有記錄,並且QueueStatus < 4.此查詢令我瘋狂!
這裏是我試過:
UPDATE Task SET SysTaskStatus = 3
WHERE SysTaskStatus <> 3 AND [SysTask] NOT IN ((SELECT tq.SysTask FROM [dbo].[TaskQueue] tq WHERE [SysTask] = tq.SysTask AND tq.[SysTaskQueueStatus] IN (1,2,3)))
UPDATE Task SET SysTaskStatus = 3
WHERE EXISTS (SELECT 1 FROM [dbo].[TaskQueue] WHERE Task.[SysTask] = [SysTask] AND [SysTaskQueueStatus] <4 HAVING COUNT(*)=0)
UPDATE Task SET SysTaskStatus = 3
WHERE 0 = (SELECT SysTask FROM [dbo].[TaskQueue] WHERE Task.[SysTask] = SysTask AND [SysTaskQueueStatus] < 4)
對於這一切查詢總是有相同的錯誤:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
是的,你的解決方案工作的很好..現在我知道問題是在觸發TASK表,我不能刪除它:/我甚至不能: UPDATE任務SET TaskStatus = 2 WHERE SysTask IN( 7,8)。 我只能做: UPDATE任務設置TaskStatus = 2 WHERE SysTask = 8 所以我必須先選擇SysTask ID ..然後爲每個單一的ID做更新..但我可以在存儲過程中嗎? – Marshall