这是一个 Sql-Server 查询,在 Postgresql 中如何实现?

Here is a Sql-Server query, How do it in Postgresql?

这是一个SQL-服务器查询,在 PostgreSQL 中如何实现?

DECLARE @p_key as int= NEXT VALUE FOR  ProductSequence;
INSERT INTO Product(ID,Name,Price) VALUES(@p_key,@Name,@Price);
INSERT INTO ProductInfo(ID,Guid,DateCreation, DateLastChange, AgentCreationID,AgentLastChangeID) VALUES(@p_key, @Guid, @DateCreation, @DateLastChange, @AgentCreationID, @AgentLastChangeID);
SELECT  @p_key;

假设 product.id 被定义为 identity 你可以用一个可写的 CTE 来做到这一点:

with new_product as (
  insert into product (name, price)
  values (...)
  returning id
)
insert into product_info (id, guid, date_creation, date_last_change, anget_creation_id, agent_last_change_id )
select id, 'guid', ...
from new_product;

否则只需使用 nextval()lastval()

insert into product 
  (id, name, price)
values 
  (nextvalue('product_id_seq'), ...);

insert into product_info 
  (id, guid, date_creation, date_last_change, anget_creation_id, agent_last_change_id )
values 
  (lastval(), ....);