2017-06-26 66 views
0

我想創建一個像下面這樣的函數,根據給定的輸入插入數據。但是我不斷收到關於不確定美元符號的錯誤。我收到美元符號未終止

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

謝謝你的迴應。這是一段經過編輯的代碼,但代碼文本之後的逗號不是問題。我越來越----------- 未終止美元報價始於123位。預計終止$$ –

+1

你使用的是什麼客戶端?..上述錯誤看起來並不是一個postgres我 –

回答

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