软件下载吧文章资讯

分类分类

PostgreSQL 数据库基础 之 存储过程循环调用方式

2024-02-07 12:09作者:下载吧

需求描述

碰到需求,需要往表里插入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;

展开全部

相关文章

说两句网友评论
    我要跟贴
    取消