Oracle函数wm_concat移植到PostgreSQL注意事项

Oracle的包(package)中的代码片段如下:

 ---处理菜单
         tempsql := 'insert into T_CALMENUFILTERCONFIG(ACCTSYSTYPE,NOTSHOWSUBMENU,ISSUE,USERID,HSAGENCY) ';
         tempsql :=tempsql||' select g.acctsystype,'''',1,t.userid,g.hsagency from t_cadataright t,t_acalmenurole g where t.roleid = g.roleid';
         tempsql :=tempsql||' and t.roleid = '||proleid||' and g.acctsystype='||pacctsystype||' and exists (select 1 from t_causer us where us.type < 1 and t.userid = us.userid) ';
         if pproxy = 1 then  -- 代理
            tempsql :=tempsql||' and g.hsagency='||phsagency||' and g.proxy=1';
           
            execute immediate tempsql;
            -- 处理隐藏菜单
            tempsql :='update T_CALMENUFILTERCONFIG c set c.notshowsubmenu = (';
            tempsql := tempsql||' select to_char(wm_concat(g.menuid)) from t_acalmenurole g where g.acctsystype ='||pacctsystype||' and g.hsagency='||phsagency||' and g.proxy=1 and g.roleid='||proleid||')';
            tempsql := tempsql||' where c.acctsystype='||pacctsystype||' and c.hsagency='||phsagency||' and c.userid in (';
            tempsql := tempsql||' select d.userid from t_cadataright d where d.roleid = '||proleid||')';
            
            execute immediate tempsql;
         else
            tempsql :=tempsql||' and nvl(g.hsagency,0)=0 and nvl(g.proxy,0)=0';
           
            execute immediate tempsql;
            -- 处理隐藏菜单行转列到用户
            tempsql :='update T_CALMENUFILTERCONFIG c set c.notshowsubmenu = (';
            tempsql := tempsql||' select to_char(wm_concat(g.menuid)) from t_acalmenurole g where g.acctsystype ='||pacctsystype||' and nvl(g.hsagency,0)=0 and nvl(g.proxy,0)=0 and g.roleid='||proleid||')';
            tempsql := tempsql||' where c.acctsystype='||pacctsystype||' and nvl(c.hsagency,0)=0  and c.userid in (';
            tempsql := tempsql||' select d.userid from t_cadataright d where d.roleid = '||proleid||')';
            
            execute immediate tempsql;

移植到PostgreSQL上的代码如下:

tempsql := 'insert into T_CALMENUFILTERCONFIG(ACCTSYSTYPE,NOTSHOWSUBMENU,ISSUE,USERID,HSAGENCY) ';
			tempsql := tempsql || ' select g.acctsystype,'''',1,t.userid,g.hsagency from t_cadataright t,t_acalmenurole g where t.roleid = g.roleid';
			tempsql := tempsql || ' and t.roleid = ' || proleid || ' and g.acctsystype=' || pacctsystype || ' and exists (select 1 from t_causer us where us.type < 1 and t.userid = us.userid) ';
			if pproxy = 1 then
				tempsql := tempsql || ' and g.hsagency=' || phsagency || ' and g.proxy=1';
				execute immediate tempsql;
				tempsql := 'update T_CALMENUFILTERCONFIG c set c.notshowsubmenu = (';
				tempsql := tempsql || ' select to_char(string_agg(menuid,'','')) from t_acalmenurole g where g.acctsystype =' || pacctsystype || ' and g.hsagency=' || phsagency || ' and g.proxy=1 and g.roleid=' || proleid || ')';
				tempsql := tempsql || ' where c.acctsystype=' || pacctsystype || ' and c.hsagency=' || phsagency || ' and c.userid in (';
				tempsql := tempsql || ' select d.userid from t_cadataright d where d.roleid = ' || proleid || ')';
				--raise notice 'tempsql is: %',tempsql; 
				execute immediate tempsql;
			else
				tempsql := tempsql || ' and nvl(g.hsagency,0)=0 and nvl(g.proxy,0)=0';
				execute immediate tempsql;
				tempsql := 'update T_CALMENUFILTERCONFIG c set c.notshowsubmenu = (';
				tempsql := tempsql || ' select to_char(string_agg(g.menuid,'','')) from t_acalmenurole g where g.acctsystype =' || pacctsystype || ' and nvl(g.hsagency,0)=0 and nvl(g.proxy,0)=0 and g.roleid=' || proleid || ')';
				tempsql := tempsql || ' where c.acctsystype=' || pacctsystype || ' and nvl(c.hsagency,0)=0  and c.userid in (';
				tempsql := tempsql || ' select d.userid from t_cadataright d where d.roleid = ' || proleid || ')';
				execute immediate tempsql;

注意:如果

string_agg(g.menuid,',')

里面的逗号使用单引号虽然编译能通过,可是在调用存储过程时报如下

错误:

ERROR:  query "SELECT tempsql || ' select to_char(string_agg(menuid,',')) from t_acalmenurole g where g.acctsystype =' || pacctsystype || ' and g.hsagency=' || phsagency || ' and g.proxy=1 and g.roleid=' || proleid || ')'" returned 2 columns
CONTEXT:  edb-spl function pkg_afsp_roletouser.handelrole(integer,integer,integer,integer) line 32 at assignment
line 34 of package body
SQL state: 42601

后来改为2个单引号即可(原因是这里的单引号需要转义,sql中的单引号'即为转义,相当于java中的反斜杠\),如上图所示。 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值