Question

I am update the record using trigger.

Am tried

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$

above procedure show error.

How to use escape character in this update query using variable? How to use escape character with variable?. Don't suggest with out variable using escape character.

Am using postgresql 9.1

EDIT

I get information form some command so i update like this in my trigger

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

Now it show in table in two lines but its end and start with '.

like

narration

 this is first'
 'this is second 

how to store with out single qutoation?

Was it helpful?

Solution

As documented in String Constants with C-style Escapes, the strings that contain escape sequences with backslashes must start with E as in E'line1\nline2' or E'\n' for a newline alone.

This E syntax has become mandatory since PostgreSQL 9.1 in its default configuration regarding standard_conforming_strings, as warned in the doc at the same place:

If the configuration parameter standard_conforming_strings is off, then PostgreSQL recognizes backslash escapes in both regular and escape string constants. However, as of PostgreSQL 9.1, the default is on, meaning that backslash escapes are recognized only in escape string constants.
This behavior is more standards-compliant, but might break applications which rely on the historical behavior, where backslash escapes were always recognized

The quote_literal function is meant to build SQL statements programmatically before they're fed to the SQL interpreter, that's why its results includes single quotes. This function is not necessary and not helpful for your use case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top