FDavidov
FDavidov

Reputation: 3667

Programmatically alter a sequence in SQL Server

I'm preparing a data migration script in which I need to alter a previously created sequence to a new value.

Here is what I tried so far:

DECLARE @Curr_Sampling_Request_ID INT ;

SELECT @Curr_Sampling_Request_ID = MAX (Sampling_Request_ID)
  FROM [MyDB].[ABC].[T_DATA_Sampling_Requests] ;

DECLARE @Update_Sequence NVARCHAR(512) ;

SET @Update_Sequence = 
       N'ALTER SEQUENCE [S_DATA_Sampling_Requests] RESTART WITH ' + 
       CAST(@Curr_Sampling_Request_ID AS VARCHAR) ;

print('Attempting: ' + @Update_Sequence) ;

EXEC @Update_Sequence ;

Upon execution I got the following error message (printed record also shown):

Attempting: ALTER SEQUENCE [S_DATA_Sampling_Requests] RESTART WITH 2848805 Msg 203, Level 16, State 2, Line 10 The name 'ALTER SEQUENCE [S_DATA_Sampling_Requests] RESTART WITH 2848805' is not a valid identifier.

Note: I also tried without the use of a temporary string without the use of a temporary string, but same error is received.

Upvotes: 0

Views: 886

Answers (1)

Whencesoever
Whencesoever

Reputation: 2306

You used your string query like it were a normal stored procedure. You should do :

EXECUTE sp_executesql @Update_Sequence

Upvotes: 1

Related Questions