Sto cercare di creare delle funzioni in postgresql (linguaggio: plpgsql) per le insert, update, delete e select.
Ho creato una funzione per la INSERT:
Codice:
CREATE FUNCTION insert_prodotto(IN p_descrizione tbl_listino.descrizione%TYPE, IN p_soggetto tbl_soggetto.descrizione%TYPE, IN p_luogo tbl_luogo.descrizione%TYPE,IN p_quantita tbl_prodotto.quantita%TYPE) RETURNS integer AS $$
DECLARE id_luogo INTEGER;
id_listino_completo INTEGER;
BEGIN
SELECT id INTO id_luogo
FROM tbl_luogo
WHERE descrizione = p_luogo;
IF id_luogo<=0
THEN RETURN -1;--error
END IF;
IF soggetto is null then
SELECT id INTO id_listino_completo
FROM view_listino
WHERE descrizione = p_descrizione AND soggetto is null;
ELSE
SELECT id INTO id_listino_completo
FROM view_listino
WHERE descrizione = p_descrizione AND soggetto = p_soggetto;
END IF;
insert into tbl_prodotto values (id_listino_completo,id_luogo,p_quantita);
Return currval();
END;
$$ LANGUAGE plpgsql;
Ora.. provo a chiamarla:
select insert_prodotto('Ciondolo Piccolo','Pinguino','PN',10);
Ma:
ERROR: function insert_prodotto(unknown, unknown, unknown, integer) does not exist
LINE 1: select insert_prodotto('Ciondolo Piccolo','Pinguino','PN',10...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Errore **********
ERROR: function insert_prodotto(unknown, unknown, unknown, integer) does not exist
Stato SQL: 42883
Suggerimento: No function matches the given name and argument types. You might need to add explicit type casts.
Carattere: 8
Come mai?? le stringhe non dovrebbero stare tra ' ' ??
Anche facendo un cast esplicito a character varying:
select insert_prodotto('Ciondolo Piccolo'::character varying,'Pinguino'::character varying,'PN'::character varying,10);
ERROR: function insert_prodotto(character varying, character varying, character varying, integer) does not exist
LINE 1: select insert_prodotto('Ciondolo Piccolo'::character varying...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Errore **********
ERROR: function insert_prodotto(character varying, character varying, character varying, integer) does not exist
Stato SQL: 42883
Suggerimento: No function matches the given name and argument types. You might need to add explicit type casts.
Carattere: 8