在 PG 中,我需要根据变量的值从不同的查询中获取结果。
我试过这样的 -
do $$
declare myVar int;
select <something> into myVar;
if myVar = 1 then
select col1 from myTable;
else
select col2 from myTable;
end if;
end $$;
这给了我:
ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
现在,我不能做 case
,因为根据 myVar,myTable 可能有列 col1 或 col2;不是都。使用 case 将失败并显示“列不存在”,因为它会考虑整个语句并注意到不存在的列。
我希望 IF-ELSE 可以帮助避免包含不正确(不存在)列的块