postgres dblink转单引号

时间:2011-07-07 19:04:11

标签: sql postgresql escaping dblink

相关链接:

这是我的错误:

ERROR:  type "e" does not exist

这是我的问题:

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

如果我使用双引号,请删除单引号的反斜杠转义符,并在SELECT语句之前删除E

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

我明白了:

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

我的查询被截断后,我也得到了ERROR。

之前我已经使用这样的dblink进行了转义,所以是否有服务器设置或我需要配置的东西?

我知道如果我在sql server本身上运行它,那么查询就可以正常工作,但不能用dblink运行。有什么想法吗?

Postgres版本8.4

2 个答案:

答案 0 :(得分:21)

尝试用\'inactive\'替换''inactive'' - 注意:两个单引号

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

替代(上一个)解决方案

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

答案 1 :(得分:4)

尝试此查询:

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