项目中遇到测试,需要造4500数据,而且需要分部门和日期,一个部门一天30条数据,剩下的铺垫数据可以一个部门一天100w左右数据,这里,每次变换部门,日期,需要操作至少300次,想到用存储过程写一个函数进行
首先,了解存储过程的语法:
CREATE [ OR REPLACE ] FUNCTION
name( [ [argmode] [ar唐山网站设计gname]argtype[ { DEFAULT | = }default_expr] [, ...] ] )
[ RETURNSrettype
| RETURNS TABLE (column_namecolumn_type[, ...] ) ]
{ LANGUAGElang_name
| WINDOW
| IMMUTABLE | STABLE | VOLATILE
| CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
| [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
| COSTexecution_cost
| ROWSresult_rows
| SETconfiguration_parameter{ TOvalue| =value| FROM CURRENT }
| AS 'definition'
| AS 'obj_file', 'link_symbol'
} ...
[ WITH (attribute[, ...] ) ]
————————————————
CREATE [ OR REPLACE ] FUNCTION--创建一个函数,若有此函数,即取代重新创建 name -------函数名称
RETURNS---函数返回类型
具体的函数声明,请参考[postgresql存储过程]
下面说我写的函数:
CREATE OR REPLACE FUNCTION "xue"."insert_into_table"()
RETURNS "pg_catalog"."void" AS $BODY$
DECLARE tmp VARCHAR(1024);
DECLARE n integer;
DECLARE i integer;
DECLARE inst_seq_no CURSOR FOR
SELECT inst_seq_no FROM t where no in (
'111','22','223','33','4358',
'233','449','315','35335');
BEGIN
RAISE NOTICE '------------start----------';
i := 30;
FOR stmt IN no LOOP
n := 30;
FOR n IN n..i LOOP
insert into test2 (NO,
test_NO,TIME,USER_NO,SEQ_NO,
NAME,USER_NO1,USER_NAME,CODE,USER_NO2,OPROR_NAME,
REVIEW_TIME,DESC,
VAL1,VAL2,DATE,UPD_TIME,DEL_FLAG) values
(nextval('seq_test2'),n,'20190910',n,stmt.seq_no,n,n,n,n,n,n,'20190910','01','',n,n,'20190910',
'20190909','0');
END LOOP;
n = n+30;
i = i+30;
END LOOP;
RAISE NOTICE '-----------finished---------';
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100`
很简单的逻辑,但是在修改了三四遍才实现,这个第二次写存储过程,很多语法不是很熟悉,要注意的如: