为什么我的 SQL OUTPUT 语句不起作用?

Why is my SQL OUTPUT statement not working?

执行以下 2 个查询时:

INSERT INTO Orders (CustomerID) 
OUTPUT INSERTED.OrderID 
VALUES ('1');

INSERT INTO Orders (CustomerID) 
OUTPUT INSERTED.* 
VALUES ('1');

我得到以下异常:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "OUTPUT"

订单 table 是使用以下方法创建的:

CREATE TABLE Orders 
(
     OrderID SERIAL PRIMARY KEY, 
     CustomerID int NOT NULL, 

     FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

我做错了什么?谢谢

SQL 服务器中的 output 等同于 PostgreSQL 中的 returning。请试试这个。

INSERT INTO Orders (CustomerID)  
values('1')
returning CustomerID;

你也可以使用*

INSERT INTO Orders (CustomerID)  
    values('1')
    returning *;

output 子句不是 standard SQL. It is a SQL Server extension 的一部分。

PostgreSQL equivalent也是非标准的returning

The optional RETURNING clause causes INSERT to compute and return value(s) based on each row actually inserted (or updated, if an ON CONFLICT DO UPDATE clause was used). This is primarily useful for obtaining values that were supplied by defaults, such as a serial sequence number. However, any expression using the table's columns is allowed. The syntax of the RETURNING list is identical to that of the output list of SELECT. Only rows that were successfully inserted or updated will be returned. For example, if a row was locked but not updated because an ON CONFLICT DO UPDATE ... WHERE clause condition was not satisfied, the row will not be returned.

没有虚拟 inserted table,它出现在插入的末尾。

test=> create table foo ( id bigserial primary key, name text );
CREATE TABLE

test=> INSERT INTO foo (name) VALUES ('test') returning *;
 id | name 
----+------
  1 | test