I am writing a PL/PGSQL function and it produces an error. I want to print the error with RAISE NOTICE statement but I don't know how to get the error code? What variable holds the last error?
This is my sample code:
IF FOUND
THEN
BEGIN
insert into app.company(dateinserted,name) values(now(),company_name) returning comnpany_id;
return company_id;
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'Insert failed with...';
return -2;
END;
ELSE
RETURN -1;
END IF;
This code will return company_id if insert was successful and print the error if it fails.