How to set value to declared variable in SQL Server -
how set value declared variable in sql server.
declare @v_sequence int, @v_sequencename nvarchar(max); set @v_sequencename = 'dbo.myseq'; exec('select @v_sequence = next value ' + @v_sequencename) select @v_sequence
here getting error:
must declare scalar variable "@v_sequence"'
please tell me how result of @v_sequence
.
you want pass value out of execute. recommend use sp_executesql
:
declare @v_sequence int, @v_sequencename nvarchar(max), @sql nvarchar(max); set @v_sequencename = 'dbo.myseq'; select @sql = 'select @v_sequence = next value ' + @v_sequencename; exec sp_executesql @sql, n'@v_sequence int output', @v_sequence = @v_sequence output; select @v_sequence;
Comments
Post a Comment