一尘不染

postgres dblink转义单引号

sql

相关链接:

这是我的错误:

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

而且由于查询被截断,我也收到错误消息。

我以前曾使用dblink进行过转义,因此是否需要服务器设置或需要配置的东西?

我知道如果我在sql服务器本身上运行查询,但该查询工作正常,但不能与dblink一起运行。有什么想法吗?

Postgres 8.4版


阅读 170

收藏
2021-03-10

共1个答案

一尘不染

尝试替换\'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))
2021-03-10