sourcecode

쿼리에서 START WITH로 시퀀스 만들기

codebag 2023. 11. 4. 10:40
반응형

쿼리에서 START WITH로 시퀀스 만들기

START WITH 값이 쿼리에서 나오는 시퀀스를 만들려면 어떻게 해야 합니까?

이런 식으로 노력하고 있습니다. CREATE SEQUENCE "Seq" INCREMENT BY 1 START WITH (SELECT MAX("ID") FROM "Table");

하지만 ORA-01722 오류가 발생합니다.

START WITH CLAUSE는 정수를 받아들입니다."Create sequence" 문을 동적으로 작성한 다음 execute immediate를 사용하여 이를 달성할 수 있습니다.

declare
    l_new_seq INTEGER;
begin
   select max(id) + 1
   into   l_new_seq
   from   test_table;

    execute immediate 'Create sequence test_seq_2
                       start with ' || l_new_seq ||
                       ' increment by 1';
end;
/

이 링크들을 확인해보세요.

http://download.oracle.com/docs/cd/B14117_01/server.101/b10759/statements_6014.htm
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/executeimmediate_statement.htm

잘 작동하는 예제가 있습니다.

declare
 ex number;
begin
  select MAX(MAX_FK_ID)  + 1 into ex from TABLE;
  If ex > 0 then
    begin
            execute immediate 'DROP SEQUENCE SQ_NAME';
      exception when others then
        null;
    end;
    execute immediate 'CREATE SEQUENCE SQ_NAME INCREMENT BY 1 START WITH ' || ex || ' NOCYCLE CACHE 20 NOORDER';
  end if;
end;

언급URL : https://stackoverflow.com/questions/3461267/create-a-sequence-with-start-with-from-query

반응형