In PostgreSQL, what is the difference between a prepared statement and a SQL or PL/pgSQL function, in terms of their purposes, advantages and disadvantages? When shall we use which?
In this very simple example, do they work the same, correct?
CREATE TABLE foo (id INT, name VARCHAR(80));
CREATE FUNCTION myfunc1(INT, VARCHAR(80)) RETURNS void AS '
INSERT INTO foo VALUES ($1, $2);
' LANGUAGE SQL;
SELECT myfunc1(3, 'ben');
CREATE FUNCTION myfunc2(INT, VARCHAR(80)) RETURNS void AS '
BEGIN
INSERT INTO foo VALUES ($1, $2);
END' LANGUAGE plpgsql;
SELECT myfunc2(3, 'ben');
PREPARE fooplan (INT, VARCHAR(80)) AS
INSERT INTO foo VALUES($1, $2);
PREPARE
EXECUTE fooplan(3, 'ben');
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…