SQL 插入最大选定值作为新记录 oracle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21633143/
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-01 01:00:32  来源:igfitidea点击:

insert maximum selected value as new record oracle

sqloracleoracle10g

提问by Samy Louize Hanna

i'm using oracle DB . and i'm trying to select max number from table and then increment it by one then re-insert it as new record for example: insert into table1(id,name) values (select max(id) from table1 + 1,'name2');

我正在使用 oracle 数据库。我正在尝试从表中选择最大数量,然后将其加一,然后将其重新插入为新记录,例如: insert into table1(id,name) values (select max(id) from table1 + 1,'name2');

but it gave me missing expression error.

但它给了我丢失的表达错误。

thanks in advance.

提前致谢。

回答by Justin Cave

INSERT INTO table1( id, name )
  SELECT max(id) + 1, 'name2'
    FROM table1

will be valid syntax. This method of generating idvalues, is a very poor approach. It does not work in a multi-user environment since many different sessions may get the same idvalue. It is also much less efficient than using a sequence.

将是有效的语法。这种生成id值的方法是一种非常糟糕的方法。它在多用户环境中不起作用,因为许多不同的会话可能会获得相同的id值。它也比使用序列效率低得多。

回答by Samy Louize Hanna

insert into table1(id,name) values ( (select max(id) from table1 ) + 1,'name2')

回答by arunb2w

While insert into table by selecting, there is no need for keyword values

通过选择插入表格时,不需要关键字 values

insert into table1(id,name) (select max(id) from table1 + 1,'name2');