Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

sql - postgres dblink escape single quote

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...