必须声明标量变量 - 设置变量等于SELECT语句

时间:2017-05-23 13:07:24

标签: sql-server stored-procedures

我想将@inputData设置为select语句的行,然后将其传递到我的DoSomething存储过程中:

DECLARE @inputData [dbo].[TestTableType];

SET @inputData = ( 
    SELECT
        1 AS Client_Id,
        20170202 AS a,
        18595 AS b,
        49.82 AS c,
        88 AS d,
        31 AS e,
        0.67 AS f,
        0 AS g,
        44 AS h,
        1827 AS i,
        5 AS j,
        87 AS k
);

EXEC DoSomething @inputData;

但是我收到了这个错误:

  

Msg 137,Level 16,State 1,Line 3
  必须声明标量变量“@inputData”。

我做错了什么?

2 个答案:

答案 0 :(得分:4)

你没有SET个表变量。你INSERT进入他们:

INSERT INTO @inputData /* Should have column list here also */
SELECT
        1 AS Client_Id,
        20170202 AS a,
        18595 AS b,
        49.82 AS c,
        88 AS d,
        31 AS e,
        0.67 AS f,
        0 AS g,
        44 AS h,
        1827 AS i,
        5 AS j,
        87 AS k
;

答案 1 :(得分:1)

尝试使用INSERT INTO。将表类型视为临时表(它基本上是)。

DECLARE @inputData AS TestTableType

INSERT INTO @inputData 
SELECT
    1 AS Client_Id,
    20170202 AS a,
    18595 AS b,
    49.82 AS c,
    88 AS d,
    31 AS e,
    0.67 AS f,
    0 AS g,
    44 AS h,
    1827 AS i,
    5 AS j,
    87 AS k
);