2014-04-03 47 views
0

我正在使用觸發器更新記錄。在Postgresql中使用帶有變量的Escape Letter

上午試圖

CREATE OR REPLACE FUNCTION fn_trg_sales2() 
    RETURNS trigger AS 
$BODY$ declare 

xNarr text=''; 

select name into xNarr from t1 where id =1; 

update t2 set narration =E narration || case when narration ='' then xNarr else 
'\n' || xNarr end where id=1 

return null; 
end; 
$BODY$ 

上述過程顯示錯誤。

如何在此更新查詢中使用escape character使用變量? 如何使用帶變量的轉義字符?不要使用轉義字符提示out變量。

正在使用的PostgreSQL 9.1

編輯

我得到的信息形成一些命令,所以我在我的觸發更新這樣

update t2 set narration =narration || case when narration ='' then xNarr else 
    quote_literal('\n') || xNarr end where id=1 

現在顯示在表中兩行但它的結束並從'開始。

narration 

this is first' 
'this is second 

如何與出單qutoation存儲?

回答

1

String Constants with C-style Escapes記錄,包含轉義序列反斜槓字符串必須E開始在E'line1\nline2'E'\n'單獨一個換行符。

E語法已在有關standard_conforming_strings其默認配置,因爲PostgreSQL的9.1變成強制性的,因爲在同一個地方在doc警告:

如果配置參數standard_conforming_strings是關閉的, 然後PostgreSQL的識別反斜槓在常規和 轉義字符串常量。但是,從PostgreSQL 9.1開始,缺省值爲 ,這意味着只有在轉義 字符串常量中才能識別反斜槓轉義。
此行爲是更符合標準,但可能會破壞依賴於歷史的行爲,其中反斜線 逃逸總是被認可

quote_literal功能是指以編程方式生成的SQL語句他們美聯儲前 應用到SQL解釋器,這就是爲什麼它的結果包括單引號。此功能不是必需的,對您的使用情況沒有幫助。