postgres dblink escape single quote

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

Related Link:

Here is my error:

ERROR:  type "e" does not exist 

Here is my query:

SELECT * FROM dblink('host=theHostName port=1234 dbname=theDBName user=theUser password=thePassword',     E'SELECT field_1,      CASE WHEN field_2 IS NOT NULL      THEN \'inactive\' ELSE \'active\'      END AS field_status      FROM the_table       ')  AS linkresults(field_1 varchar(20),field_2 varchar(8)) 

If I use double quotes, remove the backslash escape for the single quotes and remove the E before the SELECT statement

SELECT * FROM dblink('host=theHostName port=1234 dbname=theDBName user=theUser password=thePassword',     "SELECT field_1,      CASE WHEN field_2 IS NOT NULL      THEN 'inactive' ELSE 'active'      END AS field_status      FROM the_table       ")  AS linkresults(field_1 varchar(20),field_2 varchar(8)) 

I get this:

NOTICE:  identifier "SELECT ..." will be truncated 

And the I also get the ERROR as my query has been truncated.

I have escaped with dblink like this before, so is there a server setting or something I need to configure?

I know the query works just fine if I run it on the sql server itself, but not with dblink. Any thoughts?

Postgres version 8.4

回答1:

Try replacing \'inactive\' with ''inactive'' -- caution: two single quotes

   SELECT *     FROM dblink('host=theHostName port=1234 dbname=theDBName user=theUser password=thePassword',         'SELECT field_1,          CASE WHEN field_2 IS NOT NULL          THEN ''inactive'' ELSE ''active''          END AS field_status          FROM the_table           ')   AS linkresults(field_1 varchar(20),field_2 varchar(8)) 

Alternative (previous) solution

   SELECT *     FROM dblink('host=theHostName port=1234 dbname=theDBName user=theUser password=thePassword',         'SELECT field_1,          CASE WHEN field_2 IS NOT NULL          THEN E\'inactive\' ELSE E\'active\'          END AS field_status          FROM the_table           ')   AS linkresults(field_1 varchar(20),field_2 varchar(8)) 


回答2:

Try this query:

SELECT *  FROM dblink('host=theHostName port=1234 dbname=theDBName user=theUser password=thePassword',  'SELECT field_1,   CASE WHEN field_2 IS NOT NULL   THEN $$inactive$$ ELSE $$active$$   END AS field_status   FROM the_table')   AS linkresults(field_1 varchar(20),field_2 varchar(8)) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!