2017-06-26 7 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

あなたはどのクライアントを使用していますか?上記のエラーは私にポストグルを見せません –

答えて

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 ... 
-- 
関連する問題