我需要在表中插入多个值。每个查询都会更改一个值。我有这样的INSERT:
INSERT INTO myTable (id, a) Values((select nextval('seq_myTable'), 'A' + index + '_x'
其中“ a”是字符串,而“ index”应在每个循环中更改。 我知道我应该在postgressql的某个循环内执行此操作。
答案 0 :(得分:0)
无需进行循环。您可以简单地使用generate_series
knayak=# select nextval('seq_myTable'), 'A' || index || '_x' as s
FROM generate_series(1,10) as index;
nextval | s
---------+----------
1 | A1_x
2 | A2_x
3 | A3_x
4 | A4_x
5 | A5_x
6 | A6_x
7 | A7_x
8 | A8_x
9 | A9_x
10 | A10_x
(10 rows)
插入声明
INSERT INTO myTable (id, a) select nextval('seq_myTable') as id,
'A' || index || '_x' as a
FROM generate_series(1,10) as index;