我得到美元符号未终止

Posted

技术标签:

【中文标题】我得到美元符号未终止【英文标题】:I am getting Dollar sign unterminated 【发布时间】:2017-06-26 02:44:11 【问题描述】:

我想创建一个像下面这样的函数,它根据给定的输入插入数据。但我不断收到关于未确定美元符号的错误。

CREATE OR REPLACE FUNCTION test_generate 
(
  ref         REFCURSOR,
  _id      INTEGER
)
RETURNS refcursor AS $$
DECLARE
    BEGIN
        DROP TABLE IF EXISTS test_1;
        CREATE TEMP TABLE test_1 
        (
            id int,
            request_id int,
            code text
        );

        IF _id IS NULL THEN
            INSERT INTO test_1
            SELECT
                rd.id, 
                r.id, 
                rd.code 
            FROM 
                test_2 r
            INNER JOIN 
                raw_table rd 
            ON 
                rd.test_2_id = r.id
            LEFT JOIN 
                observe_test o 
            ON 
                o.raw_table_id = rd.id
            WHERE o.id IS NULL 
            AND COALESCE(rd.processed, 0) = 0;
        ELSE
            INSERT INTO test_1
            SELECT
                rd.id, 
                r.id, 
                rd.code
            FROM 
                test_2 r
            INNER JOIN 
                raw_table rd 
            ON rd.test_2_id = r.id
            WHERE r.id = _id;
        END IF;

        DROP TABLE IF EXISTS tmp_test_2_error;
        CREATE TEMP TABLE tmp_test_2_error 
        (
            raw_table_id int,
            test_2_id int,
            error text,
            record_num int
        );

        INSERT INTO tmp_test_2_error 
        (
            raw_table_id,
            test_2_id,
            error,
            record_num
        )
        SELECT DISTINCT
            test_1.id,
            test_1.test_2_id,
            'Error found ' || test_1.code,
            0
        FROM 
            test_1
        WHERE 1 = 1
        AND data_origin.id IS NULL;

        INSERT INTO tmp_test_2_error
        SELECT DISTINCT
            test_1.id,
            test_1.test_2_id,
            'Error found ' || test_1.code,
            0
        FROM 
            test_1
        INNER JOIN 
            data_origin 
        ON 
            data_origin.code = test_1.code
        WHERE dop.id IS NULL;

        DROP table IF EXISTS test_latest;
        CREATE TEMP TABLE test_latest AS SELECT * FROM observe_test WHERE 1 = 2;

        INSERT INTO test_latest
        (
            raw_table_id,
            series_id,
            timestamp

        )
        SELECT
            test_1.id,
            ds.id AS series_id,
            now()
        FROM 
            test_1
        INNER JOIN data_origin ON data_origin.code = test_1.code
        LEFT JOIN 
            observe_test o ON o.raw_table_id = test_1.id
        WHERE o.id IS NULL;

        CREATE TABLE latest_observe_test as Select * from test_latest where 1=0; 
        INSERT INTO latest_observe_test
        (
            raw_table_id,
            series_id,
            timestamp,
            time
        )
        SELECT
            t.id,
            ds.id AS series_id,
            now(),
            t.time
        FROM 
            test_latest t
        WHERE t.series_id IS DISTINCT FROM observe_test.series_id;

        DELETE FROM test_2_error re
        USING t
        WHERE t.test_2_id = re.test_2_id;

        INSERT INTO test_2_error (test_2_id, error, record_num)
        SELECT DISTINCT test_2_id, error, record_num FROM tmp_test_2_error ORDER BY error;

        UPDATE raw_table AS rd1
        SET processed = case WHEN tre.raw_table_id IS null THEN 2 ELSE 1 END
        FROM test_1 tr
        LEFT JOIN 
            tmp_test_2_error tre ON tre.raw_table_id = tr.id
        WHERE rd1.id = tr.id;

        OPEN ref FOR
        SELECT 1;
        RETURN ref;

        OPEN ref for
            SELECT o.* from observe_test o
            ;
        RETURN ref;

        OPEN ref FOR
        SELECT
            rd.id,
            ds.id AS series_id,
            now() AS timestamp,
            rd.time
        FROM test_2 r
        INNER JOIN raw_table rd ON rd.test_2_id = r.id
        INNER JOIN data_origin ON data_origin.code = rd.code
        WHERE o.id IS NULL AND r.id = _id;
        RETURN ref;
    END;
$$ LANGUAGE plpgsql VOLATILE COST 100;

我无法运行此程序。 你能帮我在哪里做错了吗?

【问题讨论】:

去掉code text,上的逗号即可。 感谢您的回复。这是经过编辑的代码,但代码文本后的逗号不是问题。我得到 ----------- 从 123 位置开始的未终止美元报价。预计终止 $$ 你使用什么客户端?.. 上面的错误在我看来不像是 postgres ***.com/questions/3499483/… 我正在使用 DbSchema 【参考方案1】:

我正在使用松鼠,并面临与您相同的问题。 直到我发现:

-- Note that if you want to create the function under Squirrel SQL,
-- you must go to Sessions->Session Properties
-- then SQL tab and change the Statement Separator from ';' to something else
-- (for intance //). Otherwise Squirrel SQL sends one piece to the server
-- that stops at the first encountered ';', and the server cannot make
-- sense of it. With the separator changed as suggested, you type everything
-- as above and end with
--     ...
--  end;
--  $$ language plpgsql
--  //
--
--  You can then restore the default separator, or use the new one for 
--  all queries ...
-- 

【讨论】:

以上是关于我得到美元符号未终止的主要内容,如果未能解决你的问题,请参考以下文章

错误:在“$BODY$”处或附近未终止的美元引号字符串

创建函数错误错误:在“$$”处或附近未终止的美元引号字符串

Liquibase : $BODY$ 或附近的未终止的美元引号字符串

带有测试容器的未终止美元报价初始化脚本

如何在bash中回显包含未转义的美元符号的变量

[Amazon](500310) 无效操作:"$$" 处或附近未终止的美元引号字符串