您可以生成 CREATE SEQUENCE 命令:
CREATE TABLE test_tab1 (
id number
);
CREATE TABLE test_tab2 (
id number
);
INSERT INTO test_tab1 VALUES (20);
INSERT INTO test_tab2 VALUES (40);
COMMIT;
DECLARE
v_max_id NUMBER;
BEGIN
FOR v_table IN (SELECT table_name
FROM user_tables
WHERE table_name IN ('TEST_TAB1', 'TEST_TAB2'))
LOOP
EXECUTE IMMEDIATE 'SELECT NVL(MAX(id), 1) FROM ' || v_table.table_name
INTO v_max_id;
dbms_output.put_line(
'CREATE SEQUENCE ' || upper(v_table.table_name) || '_SEQ START WITH ' || v_max_id || ' INCREMENT BY 1;');
END LOOP;
END;
输出:
CREATE SEQUENCE TEST_TAB1_SEQ START WITH 20 INCREMENT BY 1;
CREATE SEQUENCE TEST_TAB2_SEQ START WITH 40 INCREMENT BY 1;