2011-01-20 87 views
0

我獲得以下從下面的函數錯誤:參數傳遞到PLPGSQL功能EXECUTE

ERROR: column "_df" does not exist 

create or replace function lax()returns setof record as 
$$ 
declare 
    rs record; 
    _planunitcode int; 
    _DF VARCHAR(25); 
    str text; 
begin 
    _planunitcode:=1; 
    _DF :='role.planner'; 
    str :='select role from userplanunit where role = _DF'; 
    --str :='select role from userplanunit where role = quote_literal('DF');';quote_ident 
    for rs in execute str 
    --for rs in select role from userplanunit where role = _DF 
    loop 
    return next rs; 
    end loop; 
    return; 
end 
$$ language 'plpgsql'; 

回答

1

我糾正它是逃逸單引號

CREATE OR replace FUNCTION lax() 
RETURNS setof record AS $$ 

DECLARE rs record; 

_planunitcode INT; 

_DF VARCHAR(25); 

str TEXT; 

BEGIN 
    _planunitcode: = 1; 

    _DF : = 'role.planner'; 

    str : = 'select role from userplanunit where role ='''||_DF||''''; 
    FOR 

    rs IN 

    EXECUTE str LOOP 

    RETURN NEXT rs; 
END 

LOOP; 

RETURN;END $$ 

LANGUAGE 'plpgsql'; 
+0

的問題,謝謝你,我得到了它,並使用在我的項目中 – kallem 2011-02-28 05:58:33