PostgreSQL存储过程循环调用操作

这篇文章主要介绍了PostgreSQL存储过程循环调用方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
 
需求描述
 
碰到需求,需要往表里插入5万条数据, 打算使用存储过程,但是postgres 数据库没有建存储过程的SQL, 所以使用函数来实现.
 
表数据结构完整性要求一次插入两条记录, 两条记录相互外键约束, record1 的 partner_id 字段值是 record2 的主键id的值, record2 的 partner_id 字段值是 record1 的主键id的值.
 
实现
 
create
 or replace function creatData() returns boolean as $BODY$
declare ii integer;
declare id1 integer;
declare id2 integer;
begin
 ii = 1;
 id1 = nextval('seq_table');
 id2 = nextval('seq_table');
FOR ii IN 1..50000 LOOP
insert
 into
 table1
 values(
 id1,
 10,
 10250,
 5001,
 '2017-08-07 14:00:00',
 '2017-08-07 15:00:00',
 id2,
 true,
 864,
 16950,
 0,
 0,
 0,
 null,
 20,
 null,
 18050,
 '2017-08-07 13:55:08',
 18051,
 '2017-08-07 13:57:28',
 false,
 401,
 10,
 null,
 null,
 null,
 'DA-HZ001000003',
 '2017-08-07 13:54:08',
 '2017-08-07 13:57:28',
 10251
 );
insert
 into
 table1
 values(
 id2,
 10,
 10251,
 5001,
 '2017-08-07 14:00:00',
 '2017-08-07 15:00:00',
 id1,
 true,
 864,
 16950,
 0,
 0,
 0,
 null,
 20,
 null,
 18050,
 '2017-08-07 13:55:08',
 18051,
 '2017-08-07 13:57:28',
 false,
 401,
 10,
 null,
 null,
 null,
 'DA-HZ001000003',
 '2017-08-07 13:54:08',
 '2017-08-07 13:57:28',
 10250
);
end LOOP;
return true;
end;
$BODY$ LANGUAGE plpgsql;
【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章