简体   繁体   English

SQL(SQL/Oracle) 使用序列从选择语句插入值

[英]SQL(SQL/Oracle) insert value from a select statement using sequence

I want to insert (user_id) value from a select statement like below without identity column.我想从如下所示的 select 语句中插入 (user_id) 值,而没有标识列。 Currently this query doesn't follow the sequence number in order.目前这个查询没有按顺序跟随序列号。 Kindly advise.好心提醒。

https://dbfiddle.uk/?rdbms=oracle_18&fiddle=195728e48cc8a5a0047fec8837e9f217 https://dbfiddle.uk/?rdbms=oracle_18&fiddle=195728e48cc8a5a0047fec8837e9f217

This is answering the original version of the question.这是回答问题的原始版本

If you are asking can you have identical SQL statements to use a sequence in SQL Server and in Oracle then, no, you cannot.如果您问是否可以使用相同的 SQL 语句在 SQL Server 和 Oracle 中使用序列,那么,不,您不能。

In Oracle, the syntax is:在 Oracle 中,语法是:

INSERT INTO b_user ( user_id /*, ... */ )
  VALUES ( b_user__user_id__seq.NEXT_VAL /*, ... */ );

In SQL Server, the syntax is:在 SQL Server 中,语法是:

INSERT INTO b_user ( user_id /*, ... */ )
  VALUES ( NEXT VALUE FOR b_user__user_id__seq /*, ... */ );

In Oracle, you could wrap the call to the sequence in a function:在 Oracle 中,您可以将对该序列的调用包装在一个函数中:

CREATE FUNCTION get_next_b_user_id RETURN INT
IS
BEGIN
  RETURN b_user__user_id__seq.NEXTVAL;
END;
/

Then you could use:然后你可以使用:

INSERT INTO b_user ( user_id /*, ... */ )
  VALUES ( get_next_b_user__id__seq() /*, ... */ );

However, in SQL server you cannot use a sequence in user-defined functions so that approach in Oracle cannot be replicated.但是,在 SQL Server 中, 您不能在用户定义的函数中使用序列,因此无法复制 Oracle 中的方法。


So, in short, you are going to have to use different SQL statements for the different databases if you are using a sequence.因此,简而言之,如果您使用序列,您将不得不对不同的数据库使用不同的 SQL 语句。

If you want to use an IDENTITY column then you can get the same syntax in both.如果要使用IDENTITY列,则可以在两者中获得相同的语法。

In Oracle:在甲骨文中:

CREATE TABLE b_user (
  user_id      INT
               GENERATED ALWAYS AS IDENTITY,
  user_name    VARCHAR(250),
  user_email   VARCHAR(250),
  user_address VARCHAR(250),
  user_city    VARCHAR(50),
  user_state   VARCHAR(5),
  user_country VARCHAR(5),
  user_zip     VARCHAR(10)
);

and in SQL Server:在 SQL Server 中:

CREATE TABLE b_user (
  user_id      INT IDENTITY(1,1),
  user_name    VARCHAR(250),
  user_email   VARCHAR(250),
  user_address VARCHAR(250),
  user_city    VARCHAR(50),
  user_state   VARCHAR(5),
  user_country VARCHAR(5),
  user_zip     VARCHAR(10)
)

Then, in both databases, you can use the statement:然后,在两个数据库中,您都可以使用以下语句:

insert into b_user (
  user_name,
  user_email,
  user_address,
  user_city,
  user_state,
  user_country,
  user_zip
) values (
  'Alice',
  'alice@example.com',
  'A house',
  'A city',
  'STATE',
  'ABC',
  'ZZ0123'
);

Oracle db<>fiddle and SQL Server db<>fiddle Oracle db<>fiddleSQL Server db<>fiddle

one option is, if you know know that the column user_id has some values already inserted then when you create a sequence which you are going to use for that column一种选择是,如果您知道列 user_id 已经插入了一些值,那么当您创建要用于该列的序列时

--example you start with 3 if you know 2 is the maximum value for that column.
CREATE SEQUENCE b_user__user_id__seq  start with 3 ;

It is better to have a primary key or unique constraint in that column.最好在该列中有一个主键或唯一约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Oracle SQL:在带有 Select 语句的插入中使用序列 - Oracle SQL: Use sequence in insert with Select Statement SQL(SQL/Oracle) 从 select 语句中插入值 - SQL(SQL/Oracle) insert value from a select statement Oracle SQL-从select中选择一个值并将其用作插入的一部分 - Oracle SQL - Selecting a value from select and using it as part of the insert Oracle SQL:插入选定的值以及序列中的下一个值 - Oracle SQL: Insert selected values as well as next value from sequence 从SQL序列中获取最后一个值并将其插入到另一个表(Oracle) - Getting the last value from a sequence in SQL and insert it into another table (Oracle) SQL Server-SQL-从SELECT语句插入值 - SQL Server - SQL - INSERT VALUE from SELECT Statement Oracle SQL - 插入选择语句 - 错误 - Oracle SQL - insert into select statement - error 使用带有变量的 SELECT 语句的 SQL INSERT INTO VALUE 语句 - SQL INSERT INTO VALUE statement using a SELECT statement with a variable SQL 服务器 / Oracle:从 select 语句为非标识列插入值(插入…值((选择语句)、值 1、值 2、值 3) - SQL Server / Oracle : insert value from a select statement for an non-identity column (Insert into…values((select statement), value1, value2, value3) SQL使用select插入语句 - SQL insert into statement using select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM