我已经创建了一个表变量,并且试图在我的查询字符串变量中引用该变量。错误是告诉我在我已经声明它时声明该变量。有人可以帮我看看我要去哪里了吗?
错误:
[42000] [Microsoft] [SQL Server本机客户端11.0] [SQL Server]
必须声明标量变量“ @ Temp2”
答案 0 :(得分:0)
问题出在+ @Temp2 +
:
SET @Query = 'SELECT
cq.Id,
(SELECT COUNT(*) AS CountRow FROM (' + (SELECT Query FROM tblCustomQuery WHERE Id = 154) + ') y) AS X FROM tblCustomQuery cq WHERE Id = IN (SELECT QueryId FROM ' + @Temp2 + 'WHERE Row_Num = 1)'
更改此位以使@Temp2
内联:
SET @Query = 'SELECT
cq.Id,
(SELECT COUNT(*) AS CountRow FROM (' + (SELECT Query FROM tblCustomQuery WHERE Id = 154) + ') y) AS X FROM tblCustomQuery cq WHERE Id = IN (SELECT QueryId FROM @Temp2 WHERE Row_Num = 1)'
答案 1 :(得分:0)