错误:语法错误在或附近"返回"在存储过程 - PostgreSQL 8.1.23

时间:2016-01-15 06:38:44

标签: postgresql stored-procedures version

我的存储过程如下:

 INSERT INTO bakersfun.orderhead 

     (order_id,order_dt, customer_id, route_id, routenum, ordertype, create_station_id, create_stationtype, create_time,create_user_id,tran_time, tran_user_id,station_id)

   values 

     (nextval('bakersfun.orderhead_order_id_seq'),$1, $2, $3, $4, $5, $6, $7, LOCALTIMESTAMP, $8, 

    default, default,$9) 

   returning  order_id;

执行时我收到如下错误:

new.sql:15: ERROR: syntax error at or near "returning"
LINE 15: returning order_id;

PostgreSQL版本:PostgreSQL 8.1.23

我注意到它适用于 PostgreSQL 8.4.20

什么是替代方案,因此它可以用于 PostgreSQL 8.1.23

1 个答案:

答案 0 :(得分:0)

DECLARE 
  l_id integer;   -- change data type according to you.
BEGIN

INSERT INTO bakersfun.orderhead 

     (order_id,order_dt, customer_id, route_id, routenum, ordertype, create_station_id, create_stationtype, create_time,create_user_id,tran_time, tran_user_id,station_id)

   values 

     (nextval('bakersfun.orderhead_order_id_seq'),$1, $2, $3, $4, $5, $6, $7, LOCALTIMESTAMP, $8, 

    default, default,$9)     
  returning order_id into l_id; --< store the returned ID in local variable
   return l_id; --< return this variable
END

更多参考检查这两个链接

Is SELECT or INSERT in a function prone to race conditions?

https://dba.stackexchange.com/questions/89643/return-the-id-after-insert-or-select