我想使用变量作为语句的一部分,但是它说“ tableref”不存在。
CREATE OR REPLACE FUNCTION ff(tipo_acta integer, hasta date)
RETURNS void AS
$BODY$
DECLARE
tableref varchar;
r record;
BEGIN
if tipo_acta = 1 then
tableref = 't1';
elsif tipo_acta = 2 then tableref = 't2';
else tableref = 't3';
end if;
for r select id from tableref where somedate >= hasta loop
--
end loop;
我尝试使用EXECUTE 'select id from ' || tableref || ' where....'
但也不起作用
我以为先获取记录select id into r from t1 where ..
,然后在循环中使用它,但是似乎没有办法在这样的循环中使用记录:
FOR r LOOP
....
END LOOP;
您需要为此使用动态sql。您需要使用execute
命令在PLPG / SQL中执行此操作。
在您的代码中,它应该类似于:
EXECUTE 'SELECT id FROM ' || tableref || ' WHERE somedate >= $1'
INTO c
USING hasta;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句