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
Another way to escape a single quote is as follows. select E 'Text\'Text'; Explanation: In the above syntax, we use a select statement but this syntax is applicable for old versions of PostgreSQL string constants with E and backslash \ to escape single quotes.
In this guide, we took a look at both single and double quoting in PostgreSQL. Double quotes are used to indicate identifiers within the database, which are objects like tables, column names, and roles. In contrast, single quotes are used to indicate string literals.
PostgreSQL also accepts “escape” string constants, which are an extension to the SQL standard. An escape string constant is specified by writing the letter E (upper or lower case) just before the opening single quote, e.g., E'foo' .
The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O'Reilly, you would use two quotes in the middle instead of one. The single quote is the escape character in Oracle, SQL Server, MySQL, and PostgreSQL.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With