我没有终结美元符号

时间:2017-06-26 02:44:12

标签: postgresql plpgsql

我想创建一个类似下面的函数,根据给定的输入插入数据。但我继续收到有关未确定美元符号的错误。

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;

我无法运行此程序。 我能帮助我做错了吗?

1 个答案:

答案 0 :(得分:2)

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

-- 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 ...
--