SQL 如何使用for循环在postgresql中插入数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41133566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:01:57  来源:igfitidea点击:

How to insert data in postgresql using for loop?

sqlpostgresql

提问by Pablo Job

I would like to know how to insert data in postgresql using for loop?

我想知道如何使用 for 循环在 postgresql 中插入数据?

I want to insert 1 to 1000 in row of id..

我想在 id 行中插入 1 到 1000。

回答by Riya Bansal

Please try below code to insert values in tables using for loop.

请尝试使用以下代码在表中使用 for 循环插入值。

do $$
begin
for r in 1..1000 loop
insert into schema_name.table_name(id) values(r);
end loop;
end;
$$;

回答by Philip Tzou

Use generate_series:

使用generate_series

INSERT INTO tableName (id) SELECT * FROM generate_series(1, 1000);