0

I am having a stored procedure as follows:

 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;

While executing I am getting error as follows:

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

PostgreSQL version : PostgreSQL 8.1.23

I noticed it works on PostgreSQL 8.4.20

What is the alternative so it could work on PostgreSQL 8.1.23.

Santhucool
  • 1,615
  • 2
  • 29
  • 77

1 Answers1

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

for more reference check these two link

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

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

Community
  • 1
  • 1
Shubham Batra
  • 2,199
  • 4
  • 24
  • 43