我有一个具有以下结构的表:
widgetnumber - text
dd - text
refnumber - text
widgetnumber和refnumber需要填充相同的值...使用generate_series生成的值。
dd将是一次设置的静态值。
我有以下代码:
INSERT INTO widgets(widgetnum, dd, refnum)
SELECT i
FROM generate_series(100, 150) AS t(i),
'somestaticstring',
SELECT p
FROM generate_series(100, 150) AS t(p);
但我必须在某处出现语法错误,因为这不起作用。 我收到以下错误:
psql:addwidgets.sql:11: ERROR: syntax error at or near "'somestaticstring'"
LINE 4: 'somestaticstring',
^
我尝试了什么
我试图将静态文本周围的单引号更改为double。我也试过这个:
INSERT INTO widgets(widgetnum, dd, refnum)
SELECT i
FROM generate_series(100, 150) AS t(i),
SELECT 'somestaticstring',
SELECT p
FROM generate_series(100, 150) AS t(p);
但是这也失败了,语法错误
答案 0 :(得分:4)
使其成为select语句的一部分:
INSERT INTO widgets(widgetnum, dd, refnum)
SELECT i::text, 'somestaticstring', i::text
FROM generate_series(100, 150) AS t(i),
答案 1 :(得分:0)
您可以将其视为普通的INSERT SELECT
INSERT INTO widgets
SELECT generate_series(100,150), 'somestaticstring', generate_series(100,150)