附加或更改proc的返回

时间:2014-06-24 11:07:12

标签: postgresql plpgsql

我有以下PostgreSQL函数返回v_result,如下所示

CREATE OR REPLACE FUNCTION public.get_email_text(p_notification_id integer)
  RETURNS character varying AS 
$BODY$
DECLARE
    v_result varchar;

BEGIN  

    SELECT email_text INTO  v_result 
    FROM  public.notification 
    WHERE notification_id = p_notification_id;
    RETURN v_result;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  ALTER FUNCTION  public.get_email_text(integer) OWNER TO test;

执行此操作时:

select public.get_email_text(4673);

它产生的输出如下:

This email is to notify you that for one or more records has been Requested. Please follow the link below to go directly to this records.  <BR/> <BR/> <BR/>Records Id : Record Name : Subrecord Id : Subrecord Name<BR/><ul><a href = http://localhost:8080/myproject/ShowRecord.action?recordsVO.recordId=3324&recordId=3324&status
=Clearance Requested >3324 </a> : DEV: test record : 2999 : Clearance</ul>

在我返回v_result之前,我想从输出中删除它:

This email is to notify you that for one or more records has been Requested.

所以剩下的只剩下了:

Please follow the link below to go directly to this records ...

2 个答案:

答案 0 :(得分:1)

在您不需要时使用更简单,更便宜的普通replace()功能 更强大,更昂贵的正则表达式函数regexp_replace()(将点(.)解释为任何字符的占位符)。

另外,我建议这个更简单的sql函数:

CREATE OR REPLACE FUNCTION public.get_email_text(p_notification_id integer)
  RETURNS text AS 
$func$
   SELECT replace(email_text, 'This email is to notify you that ...', '')
   FROM   public.notification 
   WHERE  notification_id = p_notification_id
$func$  LANGUAGE sql;
ALTER FUNCTION public.get_email_text(integer) OWNER TO test;

答案 1 :(得分:0)

我用过下面的

select regexp_replace(v_result, 'This email is to notify you that for one or more titles the Clearance has been Requested.', '') INTO
        v_popup_text;