0

I wanna create a stored procedure to check if a sequence is with a given name is exist or no to create it or alter its value to max(Id) selected from given table name, So I create the below procedure but it throws a syntax error.

CREATE PROCEDURE EBPP_CORE.CREATE_OR_ALTER_SEQUENCE
    @sequence_name VARCHAR2,
    @table_name VARCHAR2
AS
  BEGIN
   DECLARE @MAX_ID_VAL NUMBER;
   DECLARE @sequence_exist NUMBER;
   SELECT @MAX_ID_VAL = CAST(ISNULL(MAX(id) + 1, 1) as nvarchar(10)) FROM @table_name ;
   SELECT @sequence_exist = COUNT(*) FROM sys.sequences  WHERE name = @sequence_name;
  IF @sequence_exist>0
   ' ALTER SEQUENCE  @sequence_name  RESTART WITH  @MAX_ID_VAL';
  ELSE
    'CREATE SEQUENCE @sequence_name  START WITH @MAX_ID_VAL  INCREMENT BY 1' ;
  END
3
  • Please add the details of the syntax error and samples about the input and output. Commented Feb 25, 2020 at 14:12
  • 4
    You will have to use dynamic sql for this. But I have to ask, why do you need a procedure to create a sequence in the first place? This sounds like something in the design has gone a bit left of center. And why do you need to restart a sequence with the current value? Also your syntax looks mostly like sql server but you have some datatypes that are NOT sql server (varchar2, number). And no idea what the body of your if statement is doing, it is only a partial statement. Commented Feb 25, 2020 at 14:15
  • The variables referenced inside the single quotes will be literals. One way would be to use Dynamic SQL and create the string correctly to execute it. Commented Feb 25, 2020 at 14:18

1 Answer 1

1

You have to use dynamic SQL to both find the current max ID and to create or alter the SEQUENCE objects. Something like this:

CREATE OR ALTER PROCEDURE EBPP_CORE.CREATE_OR_ALTER_SEQUENCE
    @sequence_name nvarchar(128),
    @table_name nvarchar(128)
AS
/*

drop table if exists foo
go
create table foo(id int)
exec EBPP_CORE.CREATE_OR_ALTER_SEQUENCE 'foo_seq','foo'

insert into foo(id) values (3)
exec EBPP_CORE.CREATE_OR_ALTER_SEQUENCE 'foo_seq','foo'

*/
BEGIN
   DECLARE @MAX_ID_VAL bigint;
   DECLARE @sequence_exist bigint;

   declare @sql nvarchar(max) = concat('select @MAX_ID_VAL = MAX(id) + 1 FROM ', quotename(@table_name));

   print @sql;
   exec sp_executesql @sql, N'@MAX_ID_VAL bigint output', @MAX_ID_VAL=@MAX_ID_VAL output;

   set @MAX_ID_VAL = coalesce(@MAX_ID_VAL,1)

   SELECT @sequence_exist = COUNT(*) FROM sys.sequences  WHERE name = @sequence_name;
    IF @sequence_exist>0
    BEGIN
     set @sql =  concat('ALTER SEQUENCE  ', quotename(@sequence_name), ' RESTART WITH  ', @MAX_ID_VAL) ;
     print @sql;
     exec (@sql);
    END
    ELSE
    BEGIN
     set @sql =  concat('CREATE SEQUENCE  ', quotename(@sequence_name), ' START WITH  ', @MAX_ID_VAL,' INCREMENT BY 1') ;
     print @sql;
     exec (@sql);
    END
END
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.