postgres - where in (list) - column does not exist

ε祈祈猫儿з 提交于 2019-12-12 05:18:42

问题


I'm coming from SQL Server and I was suprised to see that the following query does not work:

DELETE FROM user_job_titles WHERE id IN (
"c836d018-1d12-4507-a268-a4d80d6d3f54",
"d0961a90-7d31-4c4c-9c1b-671115e3d833",
"62dda420-6e62-4017-b41d-205c0aa82ead"
)

where user_job_titles has the following columns:

id
user_id
job_title_id

The error is:

ERROR:  column "c836d018-1d12-4507-a268-a4d80d6d3f54" does not exist
LINE 2: "c836d018-1d12-4507-a268-a4d80d6d3f54"

I'm using pgAdmin with latest postgresql version. Is there any other way to run this query?


回答1:


Use single quotes for string constants:

DELETE FROM user_job_titles
    WHERE id IN ('c836d018-1d12-4507-a268-a4d80d6d3f54',
                 'd0961a90-7d31-4c4c-9c1b-671115e3d833',
                 '62dda420-6e62-4017-b41d-205c0aa82ead'
                );

Double quotes are an escape character used with table and column names. Hence the error.




回答2:


You need to quote string literals with '

DELETE FROM user_job_titles 
WHERE id IN (
'c836d018-1d12-4507-a268-a4d80d6d3f54',
'd0961a90-7d31-4c4c-9c1b-671115e3d833',
'62dda420-6e62-4017-b41d-205c0aa82ead'
);

I'm coming from SQL Server and I was suprised to see that the following query does not work

Then you have SET QUOTED_IDENTIFIER AS OFF. By default it is ON.

When SET QUOTED_IDENTIFIER is ON, all strings delimited by double quotation marks are interpreted as object identifiers.

Check:

SET QUOTED_IDENTIFIER OFF;
SELECT "A"


-- The same behaviour as in Postgresql
SET QUOTED_IDENTIFIER ON;
SELECT "A"
-- Invalid column name 'A'.

LiveDemo



来源:https://stackoverflow.com/questions/35814771/postgres-where-in-list-column-does-not-exist

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