展E宝项目使用的是postgresql数据库,批量发送红包需求,需要采用存储过程来初始化红包记录数据。
创建存储过程语句有固定的架子,如下
CREATE OR REPLACE FUNCTION public.loop_insert() RETURNS void LANGUAGE plpgsqlAS $function$DECLAREi integer;BEGIN for i in 1 .. 2000000 loop insert into red_packet_pay (id, agent_no, amount, payment_acc) values (''||i, '112010302'||i, i, '20180224'||i); end loop;END;$function$
在写自己的存储过程时,只需关注3个地方就好:
1.declare 声明要在下面语句中用到的变量
2.begin end; 中间写内容
如果存储过程只是一次性的调用,那么就没有必要创建存储过程了,写sql语句块即可。
也有固定的架子,如下
DO LANGUAGE plpgsql $$DECLAREi integer;BEGIN for i in 1 .. 5 loop insert into public.a values (i, '112010302'); end loop;END;$$;
同样也只需关注declare变量和begin end中间的部分。