使用postgresql上的sql查询更新xml数据

时间:2014-11-17 17:10:54

标签: xml postgresql

我有要更新的XML数据,我只有SQL查询来执行此操作。如何做到这一点?

我用过

UPDATE TABLE 
SET ARCH = 'XML DATA' 
WHERE name='xxx'; 

但是当在shell上粘贴XML代码时,它会进行编译,因为知道代码当然有一些特殊字符,如'字符。

我在Ubuntu服务器shell上。

1 个答案:

答案 0 :(得分:1)

update TABLE NAME set  COL NAME=xml(REPLACE(OLDXML::text,'</NODE NAME>', NEW VALUE TO UPDATE ||'</NODE NAME>')) where name='xxxx'




Example.
CREATE TABLE notify_test (id integer NOT NULL , notifyxml text)

select * from notify_test

insert into notify_test values(1,'<root><from>Core Integra</from><fromId>mahesh@gm.com</fromId><to></to><cc></cc><bcc></bcc><subject>Accoutn created</subject><body><![CDATA[<div class="container" style="width:800px; margin:auto;font-family: Helvetica Neue, Helvetica, Arial, sans-serif; color: #696969;font-size:14px;"><p>Dear  {!User.Username}, </p> <p>                               Your account is created .Your login details: </p>   <p>                      Username : {!User.Email} </p><p>                      Password: {!password}   </p>  <p style="line-height:15px;">Best Regards,</p><p>Core Integra Team</p>  <img style="width:76px;height:24px;" alt="OrgUnit Small Logo" src=""></div>]]></body><replyTo /></root>')

-------------------------------------------------


CREATE OR REPLACE FUNCTION public.usf_cic_update_templatexml(
 templateid integer,
 to_mail text,
 cc_mail text)
returns integer as
$BODY$
declare xml text;

declare ccxml text;


begin
 SELECT notifyxml into xml from notify_test where id =templateid;
raise notice 'Value: %', to_mail;
raise notice 'Value: %', cc_mail;

 if to_mail!='' then

update notify_test set  notifyxml=xml(REPLACE(xml::text,'</to>', to_mail ||'</to>')) where id =templateid;

if cc_mail!='' then

SELECT notifyxml into ccxml from notify_test where id =templateid;
update notify_test set  notifyxml=xml(REPLACE(ccxml::text,'</cc>', cc_mail ||'</cc>')) where id =templateid;

end if;
end if;
return 1;
END

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;