反斜杠导致 PostgreSQL 语法错误?
我正在 PostgreSQL 的 pgAdminIII 中调试一个 sql 文件,该语句在执行时会产生语法错误:
ERROR: syntax error at or near "v"
LINE 81384: ...n.oid = c.relnamespace WHERE c.relkind IN (\'v\') AND n...
该语句:
CREATE OR REPLACE FUNCTION getnextview() RETURNS name AS ' DECLARE my_record RECORD; viewName name; BEGIN FOR my_record IN SELECT c.relname FROM pg_catalog.pg_class AS c LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace WHERE c.relkind IN (\'v\') AND n.nspname NOT IN (\'pg_catalog\', \'pg_toast\') AND pg_catalog.pg_table_is_visible(c.oid) LIMIT 1 LOOP viewName := my_record.relname; END LOOP; RETURN (viewName); END; ' LANGUAGE 'plpgsql' VOLATILE;
注意,pg_catalog 和 pg_toast 也会出错。
I am debugging a sql file in PostgreSQL's pgAdminIII, and this statement creates a syntax error when executing:
ERROR: syntax error at or near "v"
LINE 81384: ...n.oid = c.relnamespace WHERE c.relkind IN (\'v\') AND n...
The statement:
CREATE OR REPLACE FUNCTION getnextview() RETURNS name AS ' DECLARE my_record RECORD; viewName name; BEGIN FOR my_record IN SELECT c.relname FROM pg_catalog.pg_class AS c LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace WHERE c.relkind IN (\'v\') AND n.nspname NOT IN (\'pg_catalog\', \'pg_toast\') AND pg_catalog.pg_table_is_visible(c.oid) LIMIT 1 LOOP viewName := my_record.relname; END LOOP; RETURN (viewName); END; ' LANGUAGE 'plpgsql' VOLATILE;
Note, the pg_catalog and pg_toast also error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,完整的陈述确实极大地改变了情况。
您的问题是单引号的嵌套。如果您使用 PostgreSQL 的“美元引用”一切都变得容易多了:
So the full statement does change the picture substantially.
Your problem is the nesting of single quotes. If you use PostgreSQL's "dollar quoting" everything is a lot easier:
这完全一样,只是更简单:
This does the same exactly, just simpler: