简体   繁体   English

无法在 PostgreSQL 中创建存储过程:“$$”处或附近的未终止的美元引号字符串

[英]Unable to create stored procedure in PostgreSQL: unterminated dollar-quoted string at or near "$$

I am losing my mind.我正在失去理智。 In SQL Server, creating a stored procedure is very simple.在 SQL Server 中,创建存储过程非常简单。 There is a simple structure, you define it, and create.有一个简单的结构,你定义它,然后创建。 Done.完毕。 In Postgres however, I have seen so many variations on the syntax for creating a function or stored procedure, and i have yet to be able to create one at all.然而,在 Postgres 中,我看到了创建函数或存储过程的语法有很多变化,我还没有能够创建一个。 I always get errors.我总是得到错误。 Always.总是。 On any example I've tried online.在我在线尝试的任何示例中。

The example below fails with this error:下面的示例失败并显示此错误:

[Sep 3, 2020 12:26 PM] 42601: unterminated dollar-quoted string at or near "$$ DECLARE _schemaname text; _tablename text; _key text; _value text; _columns text[]; _values text[]; BEGIN SELECT pg_namespace.nspname, pg_class.relname INTO STRICT _schemaname, _tablename FROM pg_class JOIN pg_namespace ON (pg_namespace.oid = pg_class.relnamespace) WHERE pg_class.oid = $1; FOR _key IN SELECT columns.column_name FROM information_schema.columns WHERE columns.table_schema = _schemaname AND columns.table_name = _tablename ORDER BY columns.ordinal_position LOOP EXECUTE format($s$SELECT format('%%L', ((($1)::%s).%I))$s$, $1, _key) USING $2 INTO STRICT _value"

I don't know how to resolve this.我不知道如何解决这个问题。 I did some searching, and tried the suggestion of changing the $$ to $BODY$ at the top and bottom of the script, but it fails with the same error.我做了一些搜索,并尝试将脚本顶部和底部的 $$ 更改为 $BODY$ 的建议,但它失败并出现相同的错误。 What am I doing wrong?我究竟做错了什么?

Here is the stored procedure as defined on another website.这是另一个网站上定义的存储过程。

CREATE OR REPLACE FUNCTION create_insert_statement(regclass, anyelement) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
_schemaname text;
_tablename text;
_key text;
_value text;
_columns text[];
_values text[];
BEGIN
SELECT pg_namespace.nspname, pg_class.relname
INTO STRICT _schemaname, _tablename
FROM pg_class
JOIN pg_namespace
    ON (pg_namespace.oid = pg_class.relnamespace)
WHERE pg_class.oid = $1;

FOR _key IN
    SELECT columns.column_name
    FROM information_schema.columns
    WHERE columns.table_schema = _schemaname
      AND columns.table_name = _tablename
    ORDER BY columns.ordinal_position
LOOP
    EXECUTE format($s$SELECT format('%%L', ((($1)::%s).%I))$s$, $1, _key) USING $2 INTO STRICT _value;
    _columns := _columns || _key;
    _values := _values || _value;
END LOOP;
RETURN format('INSERT INTO %s (%s) VALUES (%s)', $1, array_to_string(_columns, ','), array_to_string(_values, ','));
END;
$$;

You are using the wrong database client, because the function is fine.您使用了错误的数据库客户端,因为该功能很好。

Probably your client is not hip to dollar quoting and thinks that the SQL statement ends with the first semicolon in the function body.可能您的客户不喜欢美元引用,并认为 SQL 语句以函数体中的第一个分号结尾。

You could try to avoid dollar quoting and double and quadruple the single quotes as appropriate, or you can use better client software.您可以尽量避免美元引用,并酌情将单引号加倍和四倍,或者您可以使用更好的客户端软件。

您正在使用损坏的客户端,例如 HeidiSQL,它会在将您输入的文本传输到数据库之前对其进行处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Liquibase 错误 [Postgresql]:"$BODY$" 处或附近未终止的美元引号字符串 - Liquibase error [Postgresql]: unterminated dollar-quoted string at or near "$BODY$ “$$”处或附近的未终止的美元引号字符串 - Unterminated dollar-quoted string at or near "$$ 创建函数未终止的美元引号字符串 - create function unterminated dollar-quoted string org.postgresql.util.PSQLException:错误:“ $ BODY $或附近的不加引号的字符串 - org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string at or near "$BODY$ 错误:“$BODY$”处或附近的未终止的美元引号字符串 - ERROR: unterminated dollar-quoted string at or near "$BODY$ Liquibase 例外:在或附近未终止的美元引号字符串 - The Liquibase Exception: unterminated dollar-quoted string at or near 未终止的美元引号字符串创建 PostgreSQL function - Unterminated dollar-quoted string creating PostgreSQL function SQuirreL中的PostgreSQL函数定义:用美元结尾的不加引号的字符串 - PostgreSQL function definition in SQuirreL: unterminated dollar-quoted string 从Spring应用执行sql文件时,在“ $$ DECLARE”处或附近出现PSQLException不加终止符的美元报价字符串 - PSQLException unterminated dollar-quoted string at or near "$$ DECLARE while executing sql file from spring app psycopg2在“ $ body $”或附近引发未终止的美元报价字符串错误 - psycopg2 throws unterminated dollar-quoted string error at or near “$body$”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM