将语句与临时表合并

时间:2017-07-26 12:14:31

标签: postgresql

CREATE OR REPLACE FUNCTION public.merge_test (
   r_obj refcursor,
   _ldeptid character varying
) RETURNS refcursor
   LANGUAGE 'plpgsql'  COST 100.0  VOLATILE AS $function$
BEGIN
   DROP TABLE IF EXISTS tblCumulate;    
   create temp table tblCumulate (
      lCompid varchar(10),
      lOpenCount int default 0,
      lClosedCount int default 0
   );  
   DROP TABLE IF EXISTS tblOpen;  
   create temp table tblOpen (
      lOSID SERIAL,
      lCount numeric(24,0),
      lCompid varchar(100)
   );  
   MERGE  into  tblCumulate CUM using (select lcompid,lCount from tblopen) as OP   
      on CUM.lcompid=OP.lcompid
      when matched
         then update set cum.lOpenCount=op.lcount  
      when not matched
         then insert (lCompid,lOpenCount) values op.lcompid,op.lcount);
   open r_obj for  
      select * from tblCumulate;  
   return r_obj;  
END;
$function$;

当我执行(运行)此程序时显示以下错误。

ERROR:  "tblcumulate" is not a known variable
LINE 41:  MERGE  into  tblCumulate   CUM  temp

1 个答案:

答案 0 :(得分:1)

PostgreSQL中没有MERGE语句。

考虑使用INSERT ... ON CONFLICT