I am using a function to insert values into a table in postgres. Is it possible to retrieve the inserted/updated/deleted record in same function.
I am new to postgres, so would appreciate if someone could guide or explain how to do this.
Here is my sample function:
CREATE OR REPLACE FUNCTION SampleFunction (in name varchar, in descsciption varchar, in userid varchar) RETURNS void AS
$BODY$
begin
insert into sampletable(id,categoryname,categorydesc,createdby)
values(default,name,descsciption,userid);
end
$BODY$
LANGUAGE 'plpgsql'
Use the
RETURNING
clause.e.g. (Untested):
Note the use of
RETURNS SETOF sampletable
to return the rowtype of the table, so you don't have to repeat the column definitions.For more information, see the PL/PgSQL manual.
Note that in this case you don't need PL/PgSQL at all. If you insist on having this as a function (which isn't necessary) you can just write: