I want to create upsert function that accessing CTE Table inner IF Clause, but i found Error :
ERROR: syntax error at or near "ON"
LINE 12: SELECT id , 999 FROM list ON CONFLICT (id) ^
********** Error **********
ERROR: syntax error at or near "ON" SQL state: 42601 Character: 346
Data in hasil_perhitungan
table:
----------
id + hasil
---+------
21 + 666
22 + 777
23 + 777
24 + 777
25 + 777
---+------
Data in list_di
:
---
id
---
19
20
21
---
I have to try this :
postgres syntax error at or near "ON"
and
CREATE OR REPLACE FUNCTION coba(input_data integer)
RETURNS void AS
$BODY$
BEGIN
IF EXISTS (SELECT id, CASE WHEN id = input_data THEN 'OK' END FROM hasil_perhitungan)
THEN
WITH result AS (
SELECT id FROM hasil_perhitungan)
, list AS (
SELECT id FROM list_di)
INSERT INTO hasil_perhitungan
SELECT id , 999 FROM list ON CONFLICT (id)
UPDATE SET hasil = 888;
END IF;
END;
$BODY$
LANGUAGE plpgsql;
I have expected when i execute the function with IF statement and integer parameter that exist that can accessing CTE table inner the IF Statement. so When I run function with :
SELECT coba(25);
The Function will insert and update so i get :
Data in hasil_perhitungan table :
----------
id + hasil
---+------
19 + 999
20 + 999
21 + 888
22 + 777
23 + 777
24 + 777
25 + 777
---+------