如何在单个SELECT查询中设置多个T-SQL变量?

时间:2011-08-02 19:23:34

标签: sql sql-server-2008 select set

我有3个变量:@testid@sampleid@clientid

如何通过执行此查询一次设置@sampleid@clientid

SELECT
  [sample].sampleid,
  [client].clientid
FROM
  dbo.[test]
  LEFT OUTER JOIN dbo.[sampleslice] ON dbo.[test].samplesliceid = dbo.[sampleslice].samplesliceid
  LEFT OUTER JOIN dbo.[sample] ON dbo.[sampleslice].sampleid = dbo.[sample].sampleid
  LEFT OUTER JOIN dbo.[client] ON dbo.[sample].clientid = dbo.[client].clientid
WHERE
  testid = @testid

1 个答案:

答案 0 :(得分:23)

DECLARE @sampleid YOUR_VAR_TYPE;
DECLARE @clientid YOUR_VAR_TYPE;

SELECT
   @sampleid = [sample].sampleid, 
   @clientid = [client].clientid
FROM dbo.[test]


-- The variables are now initialized. You can now use them below.above