Aij*_*jaz 2 postgresql plpgsql
我正在创建一个临时表,并从多个表中获取数据并将数据插入其中。当我尝试从表中取回数据时,出现错误:
Run Code Online (Sandbox Code Playgroud)[42601] ERROR: query has no destination for result data Hint: If you want to discard the results of a SELECT, use PERFORM instead.
do
$$
DECLARE
sampleproductId varchar;
productIds text[] := array [
'abc1',
'abc2'
];
tId varchar;
DECLARE result jsonb;
DECLARE resultS jsonb[];
begin
CREATE TEMP TABLE IF NOT EXISTS product_temp_mapping
(
accountid int,
productUID varchar,
sampleproductId text
);
FOREACH sampleproductId IN ARRAY productIds
LOOP
tId := (select id
from product.product
where uid = sampleproductId);
INSERT into product_temp_mapping (accountid, productUID, sampleproductId)
select accountid, tId, sampleproductId
from product.accountproductmap
where productId = cast(tId as int);
END LOOP;
select * from product_temp_mapping;
end ;
$$;
Run Code Online (Sandbox Code Playgroud)
这是正确的做法吗?这是我第一次使用临时表做一些事情。
当我尝试从表中取回数据时,出现错误。
DO那是因为您无法从声明中返回任何内容。您将需要一个在子句中定义返回类型的函数RETURNS。(但是您仍然不需要临时表。)
另外,PL/pgSQL 不允许SELECT没有目标。
但对于示例来说,您不需要任何这些。一个简单的查询就可以做到这一点:
SELECT a.accountid::int, p.id::varchar AS product_uid, a.sampleproductId::text
FROM product.product p
JOIN product.accountproductmap a ON a.productId = p.id::int
WHERE p.uid = ANY ('{abc1, abc2}'::text[]);
Run Code Online (Sandbox Code Playgroud)
有些演员阵容可能是不必要的。
您可以将其包装到 SQL 函数中(这不需要 PL/pgSQL)。看:
另外:使用合法的小写标识符!看:
| 归档时间: |
|
| 查看次数: |
3685 次 |
| 最近记录: |