3793360
3793360
Tahun : 2009
SQL #1
SQL #2
SQL #3
Network
myproc
SQL #1
SQL #2
CALL myproc
SQL #3
SQLCODE SQLSTATE
= 0, successful. Success: SQLSTATE
> 0, successful with warning '00000'
< 0, unsuccessful Not found: SQLSTATE
= 100, no data was found. '02000'
i.e. FETCH statement returned no Warning: SQLSTATE
data '01XXX'
Exception: Everything
else
Bina Nusantara University
14
Conditions
A condition represents a given SQLSTATE
It can be raised by any SQL statement
You can provide names to a condition to make your code more readable
Built-in names for general conditions:
SQLWARNING, SQLEXCEPTION, NOT FOUND
These map to the SQLSTATEs mentioned in the previous slide
Specific conditions:
SQLSTATE '01004‘
To assign a user-defined name to a condition:
DECLARE truncation CONDITION FOR SQLSTATE '01004'
BEGIN [ATOMIC]
DECLARE <type> HANDLER FOR <conditions>
raises <handler-action>
exception statement_1; CONTINUE point
statement_2;
statement_3;
END
UNDO or EXIT point
Bina Nusantara University
16
Flow control statements
CASE (selects an execution path (simple / searched))
IF
FOR (executes body for each row of table)
WHILE
ITERATE (forces next iteration. Similar to CONTINUE in C)
LEAVE (leaves a block or loop. "Structured Goto")
LOOP (infinite loop)
REPEAT
GOTO
RETURN
CALL (procedure call)
Useful when the final form of SQL is known only at RUN TIME
Keywords:
ƒ EXECUTE IMMEDATE - ideal for single execution SQL
ƒ PREPARE + EXECUTE - ideal for multiple execution SQL
SET stmt = 'insert into T2 values (9,9)'; Compile and execute once
EXECUTE IMMEDIATE stmt;
Bina Nusantara University
END 19
Calling from CLI
SQLCHAR *stmt = (SQLCHAR *) "CALL MEDIAN_RESULT_SET( ? )" ;
SQLDOUBLE sal = 20000.0; /* Bound to parameter marker in stmt */
SQLINTEGER salind = 0; /* Indicator variable for sal */
if (salind == SQL_NULL_DATA)
printf("Median Salary = NULL\n");
else
printf("Median Salary = %.2f\n\n", sal );
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = procName
Inline SQL PL
This is a SQL PL subset supported within a dynamic SQL
statement, triggers and UDFs
– VALUES DEPTNAME(‘000010’)
SELECT * FROM
TABLE (getEnumEmployee('E01')) T
define
CREATE TRIGGER default_class_end
qualifier for NO CASCADE BEFORE INSERT ON cl_sched
new values REFERENCING NEW AS n
FOR EACH ROW
MODE DB2SQL
WHEN (n.ending IS NULL)
SET n.ending = n.starting + 1 HOUR
optional
WHEN