如何创建我的 START WITH 值来自查询的序列?
我正在尝试这种方式:
CREATE SEQUENCE "Seq" INCREMENT BY 1 START WITH (SELECT MAX("ID") FROM "Table");
但是,我收到 ORA-01722 错误
START WITH CLAUSE 接受一个整数。您可以动态形成“创建序列”语句,然后使用执行立即执行它来实现这一点。
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;