pgsql游标批量插入数据,id基于最大值自增

CREATE OR REPLACE FUNCTION bach_save_config(c_code text,c_name text,c_type text,c_value text,c_business_type_name text,c_memo text)
RETURNS refcursor AS
$BODY$
declare
c_org_id int8;
orgid_list refcursor;
c_id_plus  int8 DEFAULT 1 ;
isexist int8;
  BEGIN
    OPEN orgid_list for EXECUTE 'select his_org_id from "comm"."hospital" group by his_org_id';
  loop
  fetch orgid_list into c_org_id;
    if found then
		select config_id into isexist from "comm"."config" where config_code=c_code and his_org_id = c_org_id LIMIT 1;
		if isexist is  null then 	
    INSERT INTO "comm"."config"("config_id", "business_type_id", "config_code", "config_name", "display_flag", "maintain_flag", "config_type", "input_code", "full_code", "precondition", "memo", "sort_order", "version", "config_value", "his_org_id", "his_creater_id", "his_creater_name", "his_create_time", "his_updater_id", "his_update_time") VALUES ((SELECT max(config_id) FROM comm.config)+c_id_plus,(SELECT business_type_id FROM comm.business_type WHERE business_type_name =c_business_type_name LIMIT 1), c_code, c_name, '1', '0', c_type, NULL, NULL, NULL, c_memo, 1, 0, c_value, c_org_id, -1, '系统管理员', now(), -1, now());
		else
		          raise notice '配置编码已经存在 ...%', c_code;  --打印消息 ---  
		end if;
    else
      exit;
    end if;
    c_id_plus = c_id_plus +1;
  end loop;
  close orgid_list;
    raise notice '配置插入完毕';
  return c_code;
  exception when others then
      raise exception 'error--(%)',sqlerrm;
end;
$BODY$
LANGUAGE plpgsql;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值