如何在SQL Server中将值设置为声明的变量。
DECLARE @V_SEQUENCE INT, @V_SEQUENCENAME NVARCHAR(MAX);
SET @V_SEQUENCENAME = 'dbo.MYSEQ';
EXEC('SELECT @V_SEQUENCE = NEXT VALUE FOR ' + @V_SEQUENCENAME)
SELECT @V_SEQUENCE
我在这里收到错误:
必须声明标量变量" @ V_SEQUENCE"'
请告诉我如何获得@V_SEQUENCE
的结果。
答案 0 :(得分:6)
您想要从执行中传递一个值。我建议您使用DECLARE @V_SEQUENCE INT, @V_SEQUENCENAME NVARCHAR(MAX), @SQL NVARCHAR(MAX);
SET @V_SEQUENCENAME = 'dbo.MYSEQ';
SELECT @SQL = 'SELECT @V_SEQUENCE = NEXT VALUE FOR ' + @V_SEQUENCENAME;
EXEC sp_executesql @SQL, N'@V_SEQUENCE INT OUTPUT', @V_SEQUENCE = @V_SEQUENCE OUTPUT;
SELECT @V_SEQUENCE;
:
{{1}}