My requirement is to retrieve into a variable, the next value for a sequence, the name of which is derived from a string and a value.
When I run the following as a test using exec sp_executesql ...
an error is reported:
Incorrect syntax near @sequenceName
What's wrong with my code?
DECLARE @nextSeqID varchar(10);
DECLARE @sql nvarchar(100);
DECLARE @eqtypeID int;
DECLARE @sequence nvarchar(50);
DECLARE @paramdef nvarchar(100);
SET @eqtypeID = 7;
SET @sequence = 'dbo.seq_eqtype_autoserno_' + CAST(@eqtypeID as nvarchar(8));
-- @sequence = dbo.seq_eqtype_autoserno_7
SET @sql = N'SELECT @nextSeqID_OUT = NEXT VALUE FOR @sequenceName';
-- @sql = SELECT @nextSeqID_OUT = NEXT VALUE FOR @sequenceName
SET @paramdef = N'@nextSeqID_OUT varchar(10) OUTPUT, @sequenceName nvarchar(50)';
-- @paramdef = @nextSeqID_OUT varchar(10) OUTPUT, @sequenceName nvarchar(50)
EXEC sp_executesql @sql, @paramdef, @sequenceName = @sequence, @nextSeqID_OUT = @nextSeqID OUTPUT;
/*
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '@sequenceName'.
*/