tags:

views:

50

answers:

3

My Issue is that the last query in this sproc is saying that I need to declare @N but I already have any ideas?

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
+1  A: 

You need to actually put the table in your WHERE clause somewhere.

For example:

UPDATE T
SET StepNum = n.[Count]
FROM Tasks T
INNER JOIN @n n ON n.SmpInstanceId = T.SmpInstanceId
Tom H.
+2  A: 

There is no reference to @n in the update query. You will need to do your update with a subquery, or with a 'from' clause (if this is sql server)

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

or

update tasks
  set stepnum = n.count
  from tasks
    inner join @n as n
      on (Tasks.SmpInstanceID = n.smpinstanceid)
Ray
THe second Query did it for me thanks alot.
Brian Benham
A: 

You have @n declared, but it is a table, and not being used in the Update clause. You need to add it to the Update clause somehow, or you need to change it to not be a table.

UPDATE Tasks
SET    StepNum = @n.Count
FROM Tasks
JOIN @n on  Tasks.SmpInstanceID = @n.smpinstanceid
Martin