3

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.

2 Answers 2

5
...
EXCEPTION
    WHEN OTHERS THEN
        RAISE NOTICE 'Insert failed with error code %', SQLSTATE;
...

Read more in the documentation.

Sign up to request clarification or add additional context in comments.

Comments

1

include SQLERRM to get an error message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.