CREATE FUNCTION function_name(arg1 type,arg2 type)
RETURNS type
As
$$
Declare
variable_name integer;
Begin
// custom logic
End;
$$;
LANGUAGE 'language-name';
SELECT * FROM function_name(value1,value2);
SELECT function_name(val1,val2);
CREATE FUNCTION stamp_user(id int, comment text) RETURNS void AS $$
#variable_conflict use_variable
DECLARE
curtime timestamp := now();
BEGIN
UPDATE users SET last_modified = curtime, comment = comment
WHERE users.id = id;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION sum_and_product(IN a int,IN b int, OUT sum int, OUT product int) AS $$
BEGIN
IF a < 2 THEN
RAISE WARNING 'information message %', now();
RAISE NOTICE 'information message %', now();
RAISE INFO 'information message %', now();
END IF;
sum := a + b;
product := a * b;
END;
$$ LANGUAGE plpgsql
do
$$
declare
f record;
begin
for f in select id, district_name from table2
loop
update table1 set name = f.district_name where key = f.id;
end loop;
end;
$$;
do
$$
declare
d record;
begin
d = 4/0;
exception when others then
raise notice 'Zero division error';
end;
$$;
create function pg_temp.testfunc() returns text as
$$ select 'hello'::text $$ language sql;