重置Oracle序列,增加50

时间:2017-01-17 17:56:52

标签: sql oracle plsql

问题是我无法将序列重置为“1”。 我创建了如下序列......

DROP SEQUENCE TEST_SEQ;
CREATE SEQUENCE TEST_SEQ MINVALUE 1 START WITH 1 INCREMENT BY 50;

执行'nextval'时,输出为1.

SELECT TEST_SEQ.NEXTVAL FROM DUAL; --> output is '1'

以下程序来自我用于重置序列的社区

CREATE OR replace PROCEDURE RESET_SEQ(p_seq_name IN VARCHAR2) 
IS 
  l_val      NUMBER; 
  l_minvalue user_sequences.min_value%TYPE; 
BEGIN 
    SELECT min_value 
    INTO   l_minvalue 
    FROM   user_sequences 
    WHERE  sequence_name = Upper(p_seq_name); 

    EXECUTE IMMEDIATE 'SELECT '||p_seq_name||'.NEXTVAL FROM DUAL' INTO l_val; 

    l_val := l_val - l_minvalue; 

    EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||'INCREMENT BY -'||l_val|| 
    'MINVALUE '||l_minvalue; 

    EXECUTE IMMEDIATE 'SELECT '||p_seq_name||'.NEXTVAL FROM DUAL' INTO l_val; 

    EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||'INCREMENT BY 50 MINVALUE ' 
    ||l_minvalue; 
END; 
/

BEGIN
RESET_SEQ('TEST_SEQ');
END;
/

当我检查nextvalue时重置序列后,我得到输出为51.但我需要输出为1.

SELECT TEST_SEQ.NEXTVAL FROM DUAL; - >输出为'51'。

只有当我删除并重新创建序列时,我才能将下一个值设为“1”。

请帮我修改上述程序,使我的输出为'1'。

2 个答案:

答案 0 :(得分:1)

您应该检查错误打印出您要执行的SQL语句。那么你就不必同时使用Stack Overflow:

EXECUTE IMMEDIATE 'SELECT '||p_seq_name||'.NEXTVAL FROM DUAL' INTO l_val; 

    l_val := l_val - l_minvalue; 

    EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||'INCREMENT BY -'||l_val|| 
    'MINVALUE '||l_minvalue; 

    EXECUTE IMMEDIATE 'SELECT '||p_seq_name||'.NEXTVAL FROM DUAL' INTO l_val; 

    EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||' INCREMENT BY 50 MINVALUE ' 
------------------------------------------------------^ space
    ||l_minvalue; 
END; 

出于这个原因,我更喜欢使用replace()来构造这样的字符串。例如:

v_sql := 'ALTER SEQUENCE [p_seq_name] INCREMENT BY -[l_val] MINVALUE [l_minvalue]';
v_sql := replace(v_sql, '[p_seq_name]', p_seq_name);
v_sql := replace(v_sql, '[l_val]', l_val);
v_sql := replace(v_sql, '[l_minvalue]', l_minvalue);

答案 1 :(得分:0)

您必须插入空格

EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||'INCREMENT BY -'||l_val|| 
'MINVALUE '||l_minvalue;

为了它

EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||' INCREMENT BY -'||l_val|| 
'MINVALUE '||l_minvalue; 

同样如此
EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||'INCREMENT BY 50 MINVALUE ' 
||l_minvalue; 

之后它应该有用。

DROP SEQUENCE TEST_SEQ;
/
CREATE SEQUENCE TEST_SEQ MINVALUE 1 START WITH 1 INCREMENT BY 50;
/
SELECT TEST_SEQ.NEXTVAL FROM DUAL; --> output is '1'
/
SELECT TEST_SEQ.NEXTVAL FROM DUAL; --> output is '51'
/
SELECT TEST_SEQ.CURRVAL FROM DUAL; --> output is '51'
/
CREATE OR replace PROCEDURE RESET_SEQ(p_seq_name IN VARCHAR2) 
IS 
l_val      NUMBER; 
l_minvalue user_sequences.min_value%TYPE; 
BEGIN 
SELECT min_value 
INTO   l_minvalue 
FROM   user_sequences 
WHERE  sequence_name = Upper(p_seq_name); 

EXECUTE IMMEDIATE 'SELECT '||p_seq_name||'.NEXTVAL FROM DUAL' INTO l_val; 

l_val := l_val - l_minvalue; 

EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||' INCREMENT BY -'||l_val|| 
'MINVALUE '||l_minvalue; 

EXECUTE IMMEDIATE 'SELECT '||p_seq_name||'.NEXTVAL FROM DUAL' INTO l_val; 

EXECUTE IMMEDIATE 'ALTER SEQUENCE '||p_seq_name||' INCREMENT BY 50 MINVALUE ' 
||l_minvalue; 
END; 
/
BEGIN
RESET_SEQ('TEST_SEQ');
END;
/
SELECT TEST_SEQ.CURRVAL FROM DUAL; --> output is '1'
/