将参数传递给plpgsql函数中的EXECUTE

发布于 2024-10-13 18:37:48 字数 577 浏览 2 评论 0原文

我从下面的函数中得到以下错误:

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';

I get following error from the function below:

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';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2024-10-20 18:37:48

我纠正了这是转义单引号的问题

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';

i rectified it is the problem of escaping the single quote

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';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文