使用立即执行在匿名块内创建序列
Creating a sequence inside an anonymous block using Execute Immediate
我正在尝试在匿名块中创建一个序列(这是一个将在多个环境中启动的脚本)并且我正在使用立即执行,这是代码:
SELECT MAX(ID_VINCULACION)
INTO vMAX_VINCULACION
FROM SA_ENTIDADES_VINCULADAS;
EXECUTE IMMEDIATE 'CREATE SEQUENCE "GPP"."SEQ_ENTIDADES_VINCULADAS" MINVALUE 1 MAXVALUE 99999999999999999999999999 INCREMENT BY 1 START WITH ' || vMAX_VINCULACION || ' NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL';
我使用 select max 作为开始,这样我可以获得最大的现有 ID,但它抛出了这个错误:
ORA-00933: SQL command not properly ended
ORA-06512: at line 40
ORA-06512: at line 40
我不知道为什么,所以我尝试使用 Execute Immediate ... Using,这是结果代码:
SELECT MAX(ID_VINCULACION)
INTO vMAX_VINCULACION
FROM SA_ENTIDADES_VINCULADAS;
EXECUTE IMMEDIATE 'CREATE SEQUENCE "GPP"."SEQ_ENTIDADES_VINCULADAS" MINVALUE 1 MAXVALUE 99999999999999999999999999 INCREMENT BY 1 START WITH :a NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL' USING vMAX_VINCULACION;
而这个又引发了另一个错误...:
ORA-01722: invalid number
我也不明白这个,因为 Max 函数 returns 是一个数字。我已经尝试启动查询,它 returns a 5.
所以我在这里很迷路,希望你能帮助我。
提前致谢。
Execute immediate 在运行时计算。我做了一个测试,它没有问题(在我的测试中,我删除了双引号,因为你不需要它们)。尝试按 dbms_output
打印 execute immediate
的结果。可能你的 select max 没有给你正确的数字。
更新
选项 NOKEEP NOSCALE GLOBAL
在 10g
中不可用
declare
v_max pls_integer;
begin
SELECT 1000 INTO v_max FROM dual;
EXECUTE IMMEDIATE 'CREATE SEQUENCE MY_SEQ MINVALUE 1 MAXVALUE 99999999999999999999999999 INCREMENT BY 1 START WITH '||v_max||'
NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL' ;
end;
/
测试
SQL> declare
v_max pls_integer;
begin
SELECT 1000 INTO v_max FROM dual;
EXECUTE IMMEDIATE 'CREATE SEQUENCE MY_SEQ MINVALUE 1 MAXVALUE 99999999999999999999999999 INCREMENT BY 1 START WITH '||v_max||'
NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL' ;
end;
/
PL/SQL procedure successfully completed.
SQL> select sequence_name, min_value, max_value from dba_sequences where sequence_name = 'MY_SEQ' ;
SEQUENCE_NAME
--------------------------------------------------------------------------------
MIN_VALUE MAX_VALUE
---------- ----------
MY_SEQ
1 1.0000E+26
SQL> SQL> select my_seq.nextval from dual ;
NEXTVAL
----------
1000
我正在尝试在匿名块中创建一个序列(这是一个将在多个环境中启动的脚本)并且我正在使用立即执行,这是代码:
SELECT MAX(ID_VINCULACION)
INTO vMAX_VINCULACION
FROM SA_ENTIDADES_VINCULADAS;
EXECUTE IMMEDIATE 'CREATE SEQUENCE "GPP"."SEQ_ENTIDADES_VINCULADAS" MINVALUE 1 MAXVALUE 99999999999999999999999999 INCREMENT BY 1 START WITH ' || vMAX_VINCULACION || ' NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL';
我使用 select max 作为开始,这样我可以获得最大的现有 ID,但它抛出了这个错误:
ORA-00933: SQL command not properly ended ORA-06512: at line 40 ORA-06512: at line 40
我不知道为什么,所以我尝试使用 Execute Immediate ... Using,这是结果代码:
SELECT MAX(ID_VINCULACION)
INTO vMAX_VINCULACION
FROM SA_ENTIDADES_VINCULADAS;
EXECUTE IMMEDIATE 'CREATE SEQUENCE "GPP"."SEQ_ENTIDADES_VINCULADAS" MINVALUE 1 MAXVALUE 99999999999999999999999999 INCREMENT BY 1 START WITH :a NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL' USING vMAX_VINCULACION;
而这个又引发了另一个错误...:
ORA-01722: invalid number
我也不明白这个,因为 Max 函数 returns 是一个数字。我已经尝试启动查询,它 returns a 5.
所以我在这里很迷路,希望你能帮助我。
提前致谢。
Execute immediate 在运行时计算。我做了一个测试,它没有问题(在我的测试中,我删除了双引号,因为你不需要它们)。尝试按 dbms_output
打印 execute immediate
的结果。可能你的 select max 没有给你正确的数字。
更新
选项 NOKEEP NOSCALE GLOBAL
在 10g
declare
v_max pls_integer;
begin
SELECT 1000 INTO v_max FROM dual;
EXECUTE IMMEDIATE 'CREATE SEQUENCE MY_SEQ MINVALUE 1 MAXVALUE 99999999999999999999999999 INCREMENT BY 1 START WITH '||v_max||'
NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL' ;
end;
/
测试
SQL> declare
v_max pls_integer;
begin
SELECT 1000 INTO v_max FROM dual;
EXECUTE IMMEDIATE 'CREATE SEQUENCE MY_SEQ MINVALUE 1 MAXVALUE 99999999999999999999999999 INCREMENT BY 1 START WITH '||v_max||'
NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL' ;
end;
/
PL/SQL procedure successfully completed.
SQL> select sequence_name, min_value, max_value from dba_sequences where sequence_name = 'MY_SEQ' ;
SEQUENCE_NAME
--------------------------------------------------------------------------------
MIN_VALUE MAX_VALUE
---------- ----------
MY_SEQ
1 1.0000E+26
SQL> SQL> select my_seq.nextval from dual ;
NEXTVAL
----------
1000