DB2 Scalar functions - RAISE_ERROR
The RAISE_ERROR function causes the statement that includes the function to return an error with the specified SQLSTATE, SQLCODE -438, and diagnostic-string.
The schema is SYSIBM.
The sqlstate value must obey the following rules for application-defined SQLSTATEs:
0
9
A
Z
00
01
02
6
H
I
7
8
If the SQLSTATE does not conform to these rules, an error occurs (SQLSTATE 428B3).
The RAISE_ERROR function always returns the null value with an undefined data type. To use this function in a context where the data type cannot be determined, a cast specification must be used to give the null returned value a data type. A CASE expression is where the RAISE_ERROR function will be most useful.
List employee numbers and education levels as Post Graduate, Graduate and Diploma. If an education level is greater than 20, raise an error.
SELECT EMPNO, CASE WHEN EDUCLVL < 16 THEN 'Diploma' WHEN EDUCLVL < 18 THEN 'Graduate' WHEN EDUCLVL < 21 THEN 'Post Graduate' ELSE RAISE_ERROR('70001', 'EDUCLVL has a value greater than 20') END FROM EMPLOYEE