2014-03-28 3 views
0

오라클 (11) 사용, 나는에 내 시퀀서 값을 업데이트 할 쿼리/쿼리 (더 저장 프로 시저)를 사용하지 :오라클 업데이트 시퀀스 값

biggest primary key in table + 1 

이 내가 무엇을 가지고 있지만, 결합이 두 가지를 couldnt한다 :

select max("ID") from ACCOUNTS;  

ALTER SEQUENCE SEQ_ACCOUNTS INCREMENT BY "how to use here the max + 1"; 
+0

단일 쿼리에서이 작업을 수행 할 수 없습니다. 일련의 SQL 문을 실행할 수 있습니다. 또는 일련의 SQL 문을 저장 프로 시저 또는 익명 PL/SQL 블록에 넣을 수도 있습니다. –

+0

@Justin Cave 단일 쿼리 일 필요는 없으며 스토어드 프로 시저가 필요하지 않습니다. – Spring

+0

익명 PL/SQL 블록을 사용할 수 있습니까? 그렇다면 저장 프로 시저를 가져 와서'CREATE OR REPLACE PROCEDURE << 프로 시저 이름 >> AS를 DECLARE로 바꿉니다. –

답변

2

example of a stored procedure that resets a sequence value은 다른 StackOverflow 스레드에서 찾을 수 있습니다. 익명의 PL/SQL 블록이 저장 프로 시저가 아닌 사용자에게 받아 들여지는 것처럼 들립니다. 이 경우에는

declare 
    l_current_max number; 
    l_current_seq number; 
    l_tmp   number; 
begin 
    select max(id) 
    into l_current_max 
    from accounts; 

    select seq_accounts.nextval 
    into l_current_seq 
    from dual; 

    -- Set the nextval of the sequence to 0 
    execute immediate 
    'alter sequence seq_accounts increment by -' || l_current_seq || ' minvalue 0'; 

    -- Fetch a value of 0 
    execute immediate 
    'select seq_accounts.nextval from dual' 
    into l_tmp; 

    -- Set the nextval of the sequence to the current max 
    execute immediate 
    'alter sequence seq_accounts increment by ' || l_current_max || ' minvalue 0'; 

    -- Fetch the current max 
    execute immediate 
    'select seq_accounts.nextval from dual' 
    into l_tmp; 

    -- Set the nextval of the sequence to the current max + 1 
    execute immediate 
    'alter sequence seq_accounts increment by 1 minvalue 0'; 
end; 

당신은 별도의 단계에서 현재 최대로 한 후 0으로 순서를 설정하는 것보다 한 단계와 같은 일을 대신 할 수 ... 약간의 수정을 할 수 있지만 나는 그것을 발견 이렇게하면 조금 더 명확 해집니다.

+0

@Spring 시퀀스의 값을 설정하는 기능이 없으므로 로터리 방식으로 수행해야합니다. 시퀀스의 스텝 크기를 변경하십시오. –