存储过程: CREATE OR REPLACE FUNCTION test_function(integer) RETURNS integer AS $$ DECLARE b_count alias for $1; BEGIN while b_count>0 loop insert into table(...) values (...); b_count = := b_count-1; end loop; 调用(插入10条数据)select test_function(10)
存储过程: CREATE OR REPLACE FUNCTION test_function(integer) RETURNS integer AS $$ DECLARE b_count alias for $1; BEGIN while b_count>0 loop insert into table(...) values (...); b_count = := b_count-1; end loop; RETURN b_count; END; $$ LANGUAGE plpgsql; 调用(插入10条数据)select test_function(10)上面没写完 不好意思
insert into tb_1 select * from tb_2如果是外界与数据库进行的数据交换,那考虑先程序把记录按某格式写进文本,然后用SQL对应数据批量导进表
用copy语句导入
如果是测试用,则你可以用存储过程循环 insert如果是外部数据,则可以考虑导入。
可以选定列名来插入嘛 如: insert into tb_1(filed1,field2...) select filed1,field2... from tb_2
postgresql 存储过程写 循环 insert怎么写的?不会用能不能给点代码给我第一次用postgresql ,啥都不会惭愧
恩 ,这样可以了,呵呵,sql文一窍不通啊,多向前辈门学习学习 :)
CREATE OR REPLACE FUNCTION test_function(integer) RETURNS integer AS
$$
DECLARE
b_count alias for $1;
BEGIN
while b_count>0 loop
insert into table(...) values (...);
b_count = := b_count-1;
end loop;
调用(插入10条数据)select test_function(10)
CREATE OR REPLACE FUNCTION test_function(integer) RETURNS integer AS
$$
DECLARE
b_count alias for $1;
BEGIN
while b_count>0 loop
insert into table(...) values (...);
b_count = := b_count-1;
end loop;
RETURN b_count;
END;
$$
LANGUAGE plpgsql;
调用(插入10条数据)select test_function(10)上面没写完 不好意思