2010-10-26 46 views
0

我的問題是,這個sproc中的最後一個查詢是說我需要聲明@N,但我已經有任何想法?爲什麼此查詢要求我在已有表格時聲明表格?

DECLARE @t TABLE 
    (Smpinstanceid UNIQUEIDENTIFIER) 

    INSERT INTO @t 

    (Smpinstanceid) 

    SELECT t.SmpInstanceid 
    FROM Tasks t 
    WHERE t.Completed IS NOT NULL and t.SmpInstanceID is not null 

DECLARE @N TABLE 
(SMPinstanceid UNIQUEIDENTIFIER,[Count] INT) 

INSERT INTO @N 
(SMPinstanceid,[COUNT]) 

    SELECT Smpinstanceid,COUNT (Smpinstanceid) AS [Count] 
         FROM @t 
         GROUP BY Smpinstanceid 




      UPDATE Tasks 
      SET StepNum = @n.Count 
      WHERE Tasks.SmpInstanceID = @n.smpinstanceid 
END 
GO 

回答

2

沒有參考在更新查詢@n。您將需要使用子查詢做你的更新,或以「從」條款(如果這是SQL Server)的

update tasks 
    SET StepNum = (select Count from @n 
        where Tasks.SmpInstanceID = @n.smpinstanceid) 

update tasks 
    set stepnum = n.count 
    from tasks 
    inner join @n as n 
     on (Tasks.SmpInstanceID = n.smpinstanceid) 
+0

第二個查詢爲我做了非常感謝。 – 2010-10-26 14:41:33

1

您需要將表放到WHERE子句中。

例如:

UPDATE T 
SET StepNum = n.[Count] 
FROM Tasks T 
INNER JOIN @n n ON n.SmpInstanceId = T.SmpInstanceId 
0

你@n聲明,但它是一個表,並且不在Update子句中使用。您需要以某種方式將其添加到Update子句,或者您需要將其更改爲不是表格。

UPDATE Tasks 
SET StepNum = @n.Count 
FROM Tasks 
JOIN @n on Tasks.SmpInstanceID = @n.smpinstanceid 
相關問題