tags:

views:

58

answers:

1

Lets consider Result of this query may 22 Ie., (Select max(screen_id)+1 from table_screen) = 22

create sequence SEQ_TEST
minvalue SELECT MAX(SCREEN_ID)+1 FROM TABLE_SCREEN
start with 25
maxvalue 999999999999999999999999999
increment by 1
nocache;

How to set the result of a query to a minvalue in sequence??

+1  A: 

You could use dynamic sql like this:

Edit: now tested code

--Test setup
create table table_screen ( screen_id INTEGER NOT NULL);
/
insert into table_screen values (1);
/
insert into table_screen values (10);
/
insert into table_screen values (21);

new block

DECLARE
    MinVal            INTEGER;
BEGIN
    SELECT MAX(SCREEN_ID)+1 INTO MinVal FROM table_screen;

    EXECUTE IMMEDIATE 'CREATE SEQUENCE seq_test START WITH ' || MinVal;
END;
/

I removed the extra parameters, just to keep it small.

Also note: that MINVALUE is used when increment is negative, according to my favourite Oracle help guy so I swap the code to create with START WITH instead as you had a positive increment.

Simeon Pilgrim
thanks for ur reply..But i need to use the select query inside the create sequence..how can i ??
why in the create sequence command? how are you running that command that cannot have that anonymous block instead?
Simeon Pilgrim
@king: This is simply the way this is done. The CREATE SEQUENCE statement does not support subqueries. START WITH, MINVALUE etc all require integers. There's not much call for anything else, really.
Jeffrey Kemp