2

我正在尝试使用两个子查询的结果来更改序列。我是 Oracle 的新手,并且坚持使用语法。如果可以的话,请帮我解决这个问题:

alter sequence COM_UPDN_BASE_FRMT_DTL_SEQ increment by ( (select max(id) from COM_UPDN_BASE_FRMT_DTL) - (select last_number from all_sequences where sequence_name = 'COM_UPDN_BASE_FRMT_DTL_SEQ') ) ;

执行此操作后,我收到“无效号码”错误。我在这里想念什么?

4

2 回答 2

1

您肯定需要 PL/SQL 和动态 SQL,但不要使用 LAST_NUMBER。

尝试这样的事情:

DECLARE
    l_max NUMBER;
    l_curr_seq_val NUMBER;
    l_new_seq_val NUMBER;
    l_curr_inc_bv number;
BEGIN
    --Get max value from table
    SELECT max(id) INTO l_max FROM COM_UPDN_BASE_FRMT_DTL;
    --get current value from seq and current increment_by from dba_sequences
    SELECT com_updn_base_frmt_dtl_seq.nextval,increment_by INTO l_curr_seq_val,l_curr_inc_bv FROM dba_sequences WHERE sequence_name = 'COM_UPDN_BASE_FRMT_DTL_SEQ' and sequence_owner=USER;
    --If there is a difference, do the fix
    if (l_curr_inc_bv != l_max-l_curr_seq_val) then
        EXECUTE IMMEDIATE
        'ALTER SEQUENCE com_updn_base_frmt_dtl_seq INCREMENT BY ' || (l_max - l_curr_seq_val);
        SELECT com_updn_base_frmt_dtl_seq.nextval INTO l_new_seq_val from dual;
        EXECUTE IMMEDIATE
        'ALTER SEQUENCE com_updn_base_frmt_dtl_seq INCREMENT BY ' || (l_curr_inc_bv);
    end if;
END;
/
于 2020-07-10T22:58:50.347 回答
-1

您应该尝试使用变量和动态 sql:

DECLARE 
    l_max NUMBER(5,0);
    l_last NUMBER(5,0);
BEGIN
    SELECT max(id) INTO l_max FROM COM_UPDN_BASE_FRMT_DTL;
    SELECT last_number INTO l_last FROM all_sequences WHERE sequence_name = 'COM_UPDN_BASE_FRMT_DTL_SEQ';
    
    EXECUTE IMMEDIATE
    'ALTER SEQUENCE com_updn_base_frmt_dtl_seq INCREMENT BY ' || (l_max - l_last);
END;
于 2020-07-10T22:27:35.777 回答