Оглавление:
Карта сайта:
Оглавление:
Карта сайта:
PostgreSQL raises an exception is used to raise the statement for reporting the warnings, errors and other type of reported message within a function or stored procedure. We are raising the exception in function and stored procedures in PostgreSQL; there are different level available of raise exception, i.e. info, notice, warning, debug, log and notice. We can basically use the raise exception statement to raise errors and report the messages; by default, the exception level is used in the raise exception. We can also add the parameter and variable in the raise exception statement; also, we can print our variable’s value.
Syntax:
.inline-pp-banner {display:block;background:#faffbd;padding:20px 15px;border:1px solid #36a67b;border-radius:3px;} .inline-fc-mob{cursor:pointer;font-weight:bold} @media only screen and (min-width: 768px){ .inline-pp-banner{display:none;} }
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
Given below is the syntax:
RAISE [LEVEL] (Level which we have used with raise exception statement.) [FORMAT]
OR
RAISE [ LEVEL] USING option (Raise statement using option) = expression
OR
RAISE;
Parameters Description:
We can raise the exception by violating the data integrity constraints in PostgreSQL. Raise exception is basically used to raise the error and report the messages.
Given below shows the raise statement-level options, which was specifying the error severity in PostgreSQL:
If we need to raise an error, we need to use the exception level after using the raise statement in it.
We can also add more detailed information about the error by using the following clause with the raise exception statement in it.
USING option = expression
We can also provide an error message that is used to find the root cause of the error easier, and it is possible to discover the error. The exception gives an error in the error code; we will identify the error using the error code; it will define either a SQL State code condition. When the raise statement does not specify the level, it will specify the printed message as an error. After printing, the error message currently running transaction is aborted, and the next raise statement is not executed.
It is used in various parameters or options to produce an error message that is more informative and readable. When we are not specifying any level, then by default, the exception level is used in the raise statement. The exception level is aborted the current transaction with a raise statement in it. The exception level is very useful and important to raise the statement to abort the current transaction in it.
Given below are the examples mentioned:
Raise an exception statement, which reports different messages.
Code:
DO $$
BEGIN
RAISE INFO 'Print the message of information %', now() ;
RAISE LOG 'Print the message of log %', now();
RAISE DEBUG 'Print the message of debug %', now();
RAISE WARNING 'Print the message of warning %', now();
RAISE NOTICE 'Print the message of notice %', now();
RAISE EXCEPTION 'Print the message of exception %', now();
END $$;
In the above example, only info, warning, notice and exception will display the information. But debug and log will not display the output information.
Raise error using raise exception.
Code:
DO $$
DECLARE
personal_email varchar(100) := 'raise@exceptions.com';
BEGIN
– First check the user email id is correct as well as duplicate or not.
– If user email id is duplicate then report mail as duplicate.
RAISE EXCEPTION 'Enter email is duplicate: %', personal_email
USING HINT = 'Check email and enter correct email ID of user';
END $$;
Raise exception by creating function.
Code:
CREATE OR REPLACE FUNCTION test_exp() RETURNS void AS\\ $$\\ BEGIN\\ raise exception USING message = 'S 167', detail = 'D 167', hint = 'H 167', errcode = 'P3333';\\ END;\\ $$ LANGUAGE plpgsql\\ ;
Below are the advantages: