增加数字的 Oracle pl/sql 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30404402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:52:10  来源:igfitidea点击:

Oracle pl/sql script which increments number

oracleplsql

提问by Marcus Leon

Looking for a way to create an Oracle script using the equivalent of java ++ syntax to increment a variable.

寻找一种使用相当于 java ++ 语法来增加变量的创建 Oracle 脚本的方法。

Ie:

IE:

int id=10
DELETE MYTABLE;
INSERT INTO MYTABLE(ID, VALUE) VALUES (id++, 'a value');
INSERT INTO MYTABLE(ID, VALUE) VALUES (id++, 'another value');
...

Trying to use a variable and not a sequence so I can rerun this multiple times with the same results.

尝试使用变量而不是序列,这样我就可以多次重新运行它并获得相同的结果。

回答by Justin Cave

PL/SQL doesn't have the ++syntactic sugar. You'd need to explicitly change the value of the variable.

PL/SQL 没有++语法糖。您需要显式更改变量的值。

DECLARE
  id integer := 10;
BEGIN
  DELETE FROM myTable;
  INSERT INTO myTable( id, value ) VALUES( id, 'a value' );
  id := id + 1;
  INSERT INTO myTable( id, value ) VALUES( id, 'another value' );
  id := id + 1;
  ...
END;

At that point, and since you want to ensure consistency, you may be better off hard-coding the idvalues just like you are hard-coding the valuevalues, i.e.

在这一点上,由于您想确保一致性,您最id好像对值进行硬编码一样对value值进行硬编码,即

BEGIN
  DELETE FROM myTable;
  INSERT INTO myTable( id, value ) VALUES( 10, 'a value' );
  INSERT INTO myTable( id, value ) VALUES( 11, 'another value' );
  ...
END;

回答by mahi_0707

You can create a SEQUENCEto increment a number.

您可以创建一个SEQUENCE来增加一个数字。

----CREATING SEQUENCE:                                                                                                                                                      

    SQL> create sequence seq_name
    2  start with 1
    3  increment by 1
    4  NOCACHE
    5  NOCYCLE
    6  ;

Sequence created.

序列已创建。

----EXECUTION:                                                                                                                                            
SQL> select seq_name.nextval from dual;   

NEXTVAL                                                                                                                                         
1

SQL> select seq_name.nextval from dual;                                                                                                                 

NEXTVAL     
2    

Also you can create a function that can be called anywhere:

你也可以创建一个可以在任何地方调用的函数:

----CREATING FUNCTION:     
create or replace function sequence_func(a_number IN Number)
RETURN Number
AS
id Number;                                                                                                                                         
Begin                                                                                                                                              
select seq_name.nextval into id from dual;    
Return id;
end;
/

Function created.

函数创建。

----EXECUTION:  
SQL> select  sequence_func(1) seq from dual;  
seq
1  

P.S : startwithand increment byvalues below can be set as per your requirement.

PS: 可以根据您的要求设置以下值的开始增量