我使用FOR loop
进行了这个PostgreSQL简单插入查询。它需要某个帐户的资产并转移并将其传递给一个函数,该函数的结果将被插入表中。
我想使用set操作而不是FOR loop
来优化此查询。
这是当前的代码。
for assetsloop in select * from "Assets" "a" where "a"."AccountId" = _accountid LOOP
for shiftsloop in select * from temp_table_shifts LOOP
insert into temp_table_ignvalues
select * from public."GetIgnitionsOrMovements"(
shiftsloop.id::uuid,
assetsloop.id::uuid,
shiftsloop.startDateTime::timestamptz,
shiftsloop.stopDateTime::timestamptz,
shiftsloop.description::varchar
);
end LOOP;
end LOOP;
我尝试过以下
for assetsloop in select * from "Assets" "a" where "a"."AccountId" = _accountid LOOP
WITH op1 AS (
SELECT * from temp_table_shifts
--RETURNING id, startDateTime, stopDateTime, description
)
insert into temp_table_ignvalues
select * from public."GetIgnitionsOrMovements"(
(select op1.id::uuid from op1),
assetsloop.id::uuid,
(select op1.startDateTime::timestamptz from op1),
(select op1.stopDateTime::timestamptz from op1),
(select op1.description::varchar from op1)
);
end LOOP;
但我可以错误地说子查询返回多行。 谢谢你的帮助