使用Long计算的Select to Next语句中的值?

时间:2011-10-31 12:04:23

标签: sql-server

我有一个查询(在sp中)需要很长时间才能执行:

e.g。

Select name, age ,token1 from Mytable where id=2 //long calc....

//later on the same sp...

select anotherCalc from Table2 where tokenId=token1 // token1 is from the prev query.

如何将第一个查询的值传递给第二个?

Ive tried : 

declare @tmp int

Select name, age ,@tmp=token1 from Mytable where id=2 

但它不起作用......

1 个答案:

答案 0 :(得分:0)

您不能将分配给变量的select与返回结果的变量混合。如果第一个查询最多只返回一行,则可以使用

SELECT @name = name,
       @age = age,
       @token1 = token1
FROM   Mytable
WHERE  id = 2

IF(@@ROWCOUNT > 1)
    RAISERROR('Multiple rows returned - Indeterminate result',16,1)

/*Return result to client*/
SELECT @name   AS name,
       @age    AS age,
       @token1 AS token1

SELECT anotherCalc
FROM   Table2
WHERE  tokenId = @token1