SQLWARNING

SQLWARNING is a subclass of SQLException. It provides information on database access warnings. Warnings are issued by a database to indicate that no error occurred, but something unusual occurred that may be of interest or concern.

Example

DECLARE
salary number(10,2);
BEGIN
salary := 8000;
IF salary > 5000 THEN
DBMS_OUTPUT.PUT('High Salary Warning. It is: ' || salary);
END IF;
END;

Output

High Salary Warning. It is: 8000

Explanation

The above SQL warning code block for Oracle declares a salary variable and assigns a value. Then it checks if the salary is greater than 5000. If it is, a warning message is outputted. It’s just a simple demonstration and SQL itself does not have a native SQLWARNING mechanism similar to SQLException, this way Oracle’s DBMS_OUTPUT.PUT() function is used to simulate a warning message.

Example

BEGIN TRY
DECLARE @warning FLOAT;
SET @warning = (SELECT MAX(column_name)FROM table_name);
IF @warning > 1000 PRINT 'WARNING: Value exceeds limit';
END TRY
BEGIN CATCH
PRINT 'ERROR: An error occurred.';
END CATCH;

Output

WARNING: Value exceeds limit

Explanation

The given SQL code block is an example of SQLWARNING. A TRY block is used to encapsulate a series of commands. The warning message ‘WARNING: Value exceeds limit’ prints if the maximum value from the specified column of a table is greater than a defined limit (1000 in this case). A CATCH block is provided to handle potential errors during execution. In this example, it will print ‘ERROR: An error occurred.’ if there’s an execution error. In a real-world application, additional error handling or appropriate responses could be added in the CATCH block.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.