Postgresql 不可能Postgres环
我有一个Postgres循环:Postgresql 不可能Postgres环,postgresql,loops,Postgresql,Loops,我有一个Postgres循环: CREATE OR REPLACE FUNCTIOn insertIds() RETURNS int4 AS ' DECLARE r RECORD; BEGIN FOR r IN SELECT id_client FROM client WHERE id_isigeo_util =153 LOOP INSERT INTO temps_production VALUES (nextval('temps_
CREATE OR REPLACE FUNCTIOn insertIds() RETURNS int4 AS '
DECLARE r RECORD;
BEGIN
FOR r IN SELECT id_client FROM client WHERE id_isigeo_util =153 LOOP
INSERT INTO temps_production VALUES (nextval('temps_production_id_seq'::regclass),(SELECT max(id_livrable) FROM livrable),0,r.id_client,0,now(),0,now(),now());
END LOOP;
return 1;
END;
' LANGUAGE plpgsql;
SELECT insertIds() as output;
这给了我一个错误:
ERROR: syntax error at or near "temps_production_id_seq"
LINE 6: ... INSERT INTO temps_production VALUES (nextval('temps_prod...
^
********** Erreur **********
ERROR: syntax error at or near "temps_production_id_seq"
État SQL :42601
Caractère : 228
我想将select中包含的每个ID复制到temps_产品的一行中,序列号位于第一个位置,但它显示错误,它似乎不理解nextval,但我需要它,因为它是序列号
编辑:关于SELECT maxid_livrable FROM livrable,我知道它不好,因为可能会出现并发问题,但我尝试从上一次SQL插入中获取返回的变量,它给了我一个错误:
INSERT INTO livrable VALUES (nextval('id_livrable_fid_seq'::regclass),'',now(),now(),'',0,0,0,0,0,0,0,0,0,0,0,0,0,(SELECT max(id_passation)+1 FROM passation),0) RETURNING id_livrable;
这就是我的循环的外观,之后请注意id_livarable,它应该是来自上一个sql插入的变量…:
CREATE OR REPLACE FUNCTIOn insertIds() RETURNS int4
AS
$body$ --<< start of the "dollar quoting"
DECLARE r RECORD;
BEGIN
FOR r IN SELECT id_client FROM client WHERE id_isigeo_util =153 LOOP
INSERT INTO temps_production VALUES (nextval('temps_production_id_seq'::regclass),id_livrable,r.id_client,0,0,now(),0,now(),now());
END LOOP;
return 1;
END;
$body$ --< end of the "dollar quoting"
LANGUAGE plpgsql;
SELECT insertIds() as output;
我试过这个:
DECLARE myid livrable.id_livrable%TYPE;
INSERT INTO livrable VALUES (nextval('id_livrable_fid_seq'::regclass),'',now(),now(),'',0,0,0,0,0,0,0,0,0,0,0,0,0,(SELECT max(id_passation)+1 FROM passation),0) RETURNING id_livrable INTO myid;
但错误是:
ERROR: syntax error at or near "livrable"
LINE 2: DECLARE myid livrable.id_livrable%TYPE;
^
********** Erreur **********
ERROR: syntax error at or near "livrable"
État SQL :42601
Caractère : 15
编辑2:它最终是这样工作的,案件结束了谢谢你我也会做你提供的代码:
CREATE OR REPLACE FUNCTIOn insertIds() RETURNS int4
AS
$body$ --<< start of the "dollar quoting"
DECLARE r RECORD;
DECLARE myid livrable.id_livrable%TYPE;
BEGIN
INSERT INTO livrable VALUES (nextval('id_livrable_fid_seq'::regclass),'',now(),now(),'',0,0,0,0,0,0,0,0,0,0,0,0,0,(SELECT max(id_passation)+1 FROM passation),0) RETURNING id_livrable INTO myid;
FOR r IN SELECT id_client FROM client WHERE id_isigeo_util =153 LOOP
INSERT INTO temps_production VALUES (nextval('temps_production_id_seq'::regclass),myid,r.id_client,0,0,now(),0,now(),now());
END LOOP;
return 1;
END;
$body$ --< end of the "dollar quoting"
LANGUAGE plpgsql;
SELECT insertIds() as output;
您的问题是嵌套的单引号 为避免该使用出现问题,请执行以下操作: 在insert语句中显式声明列也是一种很好的编码方式:
insert into temps_production (id, ....)
select ...
好的,谢谢你,我正在尝试,但是插入像这样单独工作。如果你使用序列,为什么要使用select max?@nick:请查看我的编辑。您的函数是不必要的complexOk,是的,我同意,selectmax不正确,但我试图从以前的SQL插入中获取名为“id_livarable”的返回变量,但循环随后不接受或识别它。插入可能返回多个id,但是你的函数被定义为returns int,所以你不能从该函数返回一个以上的值。我编辑了我的消息,你有时使用返回变量吗?我无法将返回的变量注入到上一个INSERT查询的循环中,谢谢
CREATE OR REPLACE FUNCTIOn insertIds() RETURNS int4
AS
$body$ --<< start of the "dollar quoting"
DECLARE r RECORD;
BEGIN
FOR r IN SELECT id_client FROM client WHERE id_isigeo_util =153 LOOP
INSERT INTO temps_production VALUES (nextval('temps_production_id_seq'::regclass),(SELECT max(id_livrable)+1 FROM livrable),0,r.id_client,0,now(),0,now(),now());
END LOOP;
return 1;
END;
$body$ --< end of the "dollar quoting"
LANGUAGE plpgsql;
SELECT insertIds() as output;
CREATE OR REPLACE FUNCTIOn insertIds() RETURNS int4
AS
$body$
DECLARE
l_count bigint;
BEGIN
-- ATTENTION:
-- this will not work correctly with concurrent transactions on the livrable table!
SELECT max(id_livrable)+1
into l_count
FROM livrable;
INSERT INTO temps_production
select nextval('temps_production_id_seq'),
l_count,
0,
c.id_client,
0,
now(),
0,
now(),
now()
FROM client c
WHERE id_isigeo_util = 153;
return 1;
END;
$body$
LANGUAGE plpgsql;
SELECT insertIds() as output;
insert into temps_production (id, ....)
select ...