3

I'm trying to increment a sequence by the grater value from an id column (numeric type but not int). For this I've tried to declare an int variable and selecting into it the max value from the table like this:

declare
        last_id int;
begin
        select max(id) into last_id from my_table;
        alter sequence my_table_seq increment by last_id;    
end;

However, I'm getting this error

ORA-06550: line 2, column 19:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
  := . ( @ % ; not null range default character

I'm using Oracle 11g. Here's a screenshot

4
  • 2
    If you want to put DDL in a PL/SQL block, you need to use dynamic SQL and you can't use variables. You'd need to build an appropriate SQL statement in a string and EXECUTE IMMEDIATE that. My guess is that you're trying to do something like the reset_sequences procedure here does asktom.oracle.com/pls/asktom/… Commented Aug 11, 2015 at 14:36
  • You want the sequence to increment by the value of max(id) ? Commented Aug 11, 2015 at 14:50
  • @JustinCave did not know that. However I'm getting the error in the variable declaration. I've tried using execute immediate but I'm getting the same error Commented Aug 11, 2015 at 14:50
  • @DavidAldridge exactly. Commented Aug 11, 2015 at 14:58

2 Answers 2

1
declare
     last_id number;
begin
     select max(id) into last_id from my_table;
     execute immediate 'alter sequence my_table_seq increment by ' || to_char(last_id);    
end;

Use any of oracle Numeric Data Types instead of int

Use execute immediate for any DDN in anonymous block

Sign up to request clarification or add additional context in comments.

6 Comments

You can't use bind variables in DDL.
I'm still getting the error. The error occurs in the declare statement. The error code is PLS-00103
@Praveen same thing.Here is a screenshot
@Nicolas Schejtman I tested it locally by creating table, sequence and execute the above query, worked fine for me.
@Praveen must be a problem with the db management software then?
|
1

Created similar objects and it works as expected.

Suggest you to try the below:

1.Change your column to from ID to something_id.

2.Remove double quotes from schema and table name (just give schema.table_name)

3.Ensure you do not miss a semi colon at the end of statement and all variables declared properly.

If still doesn't work then go for trial and error method.

  1. Just take simple block and try printing the ID value alone.

  2. Then keep adding your logic (assign value to last_id and try print last_id).

  3. Then finally add your execute immediate statement and see if it works.

Create table general_discount_type
(id number);
    -- Table created.

Begin
insert into general_discount_type values(101);
insert into general_discount_type values(102);
insert into general_discount_type values(103);
insert into general_discount_type values(104);
End;
-- Statement processed.

Create sequence general_discount_type_seq
start with 1
increment by 1
NOCACHE
NOCYCLE;
-- Sequence created.

 Declare 
 last_id number;
 Begin
 select max(id) into last_id from MAHI_SCHEMA.general_discount_type;
 execute immediate 'alter sequence MAHI_SCHEMA.general_discount_type_seq increment by ' 
 || to_char(last_id);
 dbms_output.put_line('Value in last_id is : ' || last_id);
 End;
  -- Value in last_id is : 104

Output

OUTPUT

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.