Stored Procedure Language
Stored Procedure Language
EXAMPLE:
LANGUAGE value of SQL and the BEGIN...END block, which forms the procedure body, are particular to an SQL
procedure
1)The stored procedure name is UPDATE_SALARY_1.
2)The two parameters have data types of CHAR(6) and INTEGER. Both are input parameters.
3)LANGUAGE SQL indicates that this is an SQL procedure, so a procedure body follows the other
parameters.
4)The procedure body consists of a single SQL UPDATE statement, which updates rows in the
employee table.
Some Valid SQL Procedure Body Statements
CASE statement
FOR statement
GOTO statement
IF statement
ITERATE statement
RETURN statement
WHILE statement
• Invoking Procedures
Can invoke Stored procedure stored at the location of the database by using the
SQL CALL statement
IF <condition> THEN
<statement(s)>
ELSE
<statement(s)>
END IF;
Loops
LOOP
……
EXIT WHEN <condition>
……
END LOOP;
EXAMPLE :
CREATE PROCEDURE UPDATE_SALARY_IF
(IN employee_number CHAR(6), IN rating SMALLINT)
LANGUAGE SQL
BEGIN
SET counter = 10;
WHILE (counter > 0) DO
IF (rating = 1)
THEN UPDATE employee
SET salary = salary * 1.10, bonus = 1000
WHERE empno = employee_number;
ELSEIF (rating = 2)
THEN UPDATE employee
SET salary = salary * 1.05, bonus = 500
WHERE empno = employee_number;
ELSE UPDATE employee
SET salary = salary * 1.03, bonus = 0
WHERE empno = employee_number;
END IF;
SET counter = counter – 1;
END WHILE;
END
@
EXAMPLE :
The procedure receives a department number as an input parameter. A WHILE statement in the procedure
body fetches the salary and bonus for each employee in the department. An IF statement within the
WHILE statement updates salaries for each employee depending on number of years of service and
current salary. When all employee records in the department have been processed, the FETCH statement
that retrieves employee records receives SQLSTATE 20000. A not_found condition handler makes the
search condition for the WHILE statement false, so execution of the WHILE statement ends.
CREATE PROCEDURE BUMP_SALARY_IF (IN deptnumber SMALLINT)
LANGUAGE SQL
BEGIN
DECLARE v_salary DOUBLE;
DECLARE v_years SMALLINT;
DECLARE v_id SMALLINT;
DECLARE at_end INT DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';