我正在使用 SQL 存储过程和动态 SQL。当我尝试调试一切正常时,直到我打印我的@columnName。
这是导致麻烦的代码部分:
DECLARE @query NVARCHAR(2000)
DECLARE @columnName NVARCHAR(250)
SELECT @query = 'SELECT myTable.value FROM myTable WHERE myConditions'
EXECUTE sp_executesql @query, N'@columnName NVARCHAR(30) OUTPUT', @columnName OUTPUT
PRINT N'query='+@query
set @query = ''
PRINT N'query after reset='+@query
PRINT N'columnName='+@columnName
PRINT N'any other message'
Select @query = 'SELECT myValues AS '''+@columnName+''',
FROM myTable2
WHERE myConditions';
EXEC(@query);
这是输出消息:
query=SELECT myTable.value FROM myTable WHERE myConditions
query after reset=
any other message
那么,我的columnName=
输出在哪里?
编辑
SQL 版本: Microsoft SQL Server 2012 - 11.0.2100.60 (X64) Feb 10 2012 19:39:15 版权所有 (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (Hypervisor)