1

How can I retrieve the value from the following dynamic SQL and assign it to a variable?

SET @SQLString = N'SELECT TOP 1 D'+CAST(DAY(@dateTime)AS varchar(MAX)) 
               + ' FROM TabelForMonthCurrent'
               + ' WHERE empContractID=' + CAST(@pEmpContractID AS nvarchar(MAX))
               + ' AND Year=' + CAST(YEAR(@dateTime) AS nvarchar(MAX)) 
               + ' AND Month=' + CAST(MONTH(@dateTime) AS nvarchar(MAX))
4

1 回答 1

1

将您的动态 SQL 更改为SET @SQLString = N'SELECT @result =...

然后使用sp_executesql

DECLARE @result NVARCHAR(MAX)
EXEC sp_executesql
    @query = @SQLString, 
    @params = N'@result NVARCHAR(MAX) OUTPUT', 
    @result = @result OUTPUT
于 2013-10-01T09:36:37.223 回答