SlideShare a Scribd company logo
Stored Procedure Language
Stored Procedure Overview
•Stored Procedure is a function in a shared library accessible
to the database server
•can also write stored procedures using languages such as C
or Java
•Advantages of stored procedure : Reduced network traffic
•The more SQL statements that are grouped together for
execution, the larger the savings in network traffic
Normal Database
Applications using
stored procedures
Writing Stored Procedures
• Allows local variables, loops, procedures, examinationof one tuple ar a time.
Rough Form
DECLARE
Declarations
BEGIN
Executable statements
END
@
 The DECLARE portion is optional
 Alternate terminating character in DB2 CLP scripts: ('@'), needed for ending and
running.
Simplest Form : Sequence of Modifications
Employee( name , ssn, salary)
BEGIN
INSERT INTO EMPLOYEE VALUES (‘Sharad’, 123, 234);
DELETE FROM EMPLOYEE WHERE ssn = 234;
END
@
To process the DB2 CLP script from the command line:
db2 -tdterm-char -vf script-name
Writing Stored Procedures
 Tasks performed by the client application
 Tasks performed by the stored procedure, when invoked
 The CALL statement
 Explicit parameter to be defined :
 IN: Passes a value to the stored procedure from the client application
 OUT: Stores a value that is passed to the client application when the stored procedure terminates.
 INOUT : Passes a value to the stored procedure from the client application, and returns a value to the
Client application when the stored procedure terminates
CREATE OR REPLACE PROCEDURE <name> (<arglist>) AS <declarations>
BEGIN
<procedure statements>
END
@
EXAMPLE:
CREATE PROCEDURE UPDATE_SALARY_1 (1)
(IN EMPLOYEE_NUMBER CHAR(6), (2)
IN RATE INTEGER) (2)
LANGUAGE SQL (3)
BEGIN
UPDATE EMPLOYEE (4)
SET SALARY = SALARY * (1.0 * RATE / 100.0 )
WHERE SSN = EMPLOYEE_NUMBER;
END
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
• Nested SQL Procedures:
To call a target SQL procedure from within a caller SQL procedure, simply include
a CALL statement with the appropriate number and types of parameters in your
caller.
CREATE PROCEDURE NEST_SALES(OUT budget DECIMAL(11,2))
LANGUAGE SQL
BEGIN
DECLARE total INTEGER DEFAULT 0;
SET total = 6;
CALL SALES_TARGET(total);
SET budget = total * 10000;
END
CONDITIONAL STATEMENTS:
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';
-- CAST salary as DOUBLE because SQL procedures do not support DECIMAL
DECLARE C1 CURSOR FOR
SELECT id, CAST(salary AS DOUBLE), years
FROM staff;
DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
OPEN C1;
FETCH C1 INTO v_id, v_salary, v_years;
WHILE at_end = 0 DO
IF (v_salary < 2000 * v_years)
THEN UPDATE staff
SET salary = 2150 * v_years
WHERE id = v_id;
ELSEIF (v_salary < 5000 * v_years)
THEN IF (v_salary < 3000 * v_years)
THEN UPDATE staff
SET salary = 3000 * v_years
WHERE id = v_id;
ELSE UPDATE staff
SET salary = v_salary * 1.10
WHERE id = v_id;
END IF;
ELSE UPDATE staff
SET job = 'PREZ'
WHERE id = v_id;
END IF;
FETCH C1 INTO v_id, v_salary, v_years;
END WHILE;
CLOSE C1;
END

More Related Content

PPT
PPTX
Unit 3(rdbms)
PPTX
Unit 3(rdbms)
PPTX
Procedures and triggers in SQL
DOC
Subqueries views stored procedures_triggers_transactions
PPTX
Unit 3
PPT
Less09 Data
PPT
Module04
Unit 3(rdbms)
Unit 3(rdbms)
Procedures and triggers in SQL
Subqueries views stored procedures_triggers_transactions
Unit 3
Less09 Data
Module04

Similar to stored.ppt (20)

PPTX
Dynamic and Embedded SQL for db practices.pptx
PPTX
SQL, Embedded SQL, Dynamic SQL and SQLJ
PPTX
Stored procedures
PPT
oracle pl-sql lec 7 oracle pl-sql lec 7 plsql Lec07.ppt
PPTX
embedded-static-&dynamic
PPTX
Sql Functions And Procedures
PPTX
MS SQLSERVER:Sql Functions And Procedures
PPTX
MS SQL SERVER: Sql Functions And Procedures
PPTX
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
PPTX
PDF
2013 Collaborate - OAUG - Presentation
PPTX
Stored procedures
PDF
Sherlock holmes for dba’s
PPT
Chapter09
PPTX
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
PPTX
PL_SQL_1.pptx fvbxcfbhxdfgh .
PDF
Meg bernal insight2014 4219
PDF
Assignment#08
Dynamic and Embedded SQL for db practices.pptx
SQL, Embedded SQL, Dynamic SQL and SQLJ
Stored procedures
oracle pl-sql lec 7 oracle pl-sql lec 7 plsql Lec07.ppt
embedded-static-&dynamic
Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Procedures
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2013 Collaborate - OAUG - Presentation
Stored procedures
Sherlock holmes for dba’s
Chapter09
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
PL_SQL_1.pptx fvbxcfbhxdfgh .
Meg bernal insight2014 4219
Assignment#08

More from YashaswiniSrinivasan1 (9)

PPTX
1-161103092724.pptx
PPTX
datetimefuction-170413055211.pptx
PPTX
introductionofssis-130418034853-phpapp01.pptx
PPTX
thegrowingimportanceofdatacleaning-211202141902.pptx
PPT
Chapter5.ppt
PPT
database.ppt
PPTX
ms-sql-server-150223140402-conversion-gate02.pptx
PPT
lecture-sql.ppt
1-161103092724.pptx
datetimefuction-170413055211.pptx
introductionofssis-130418034853-phpapp01.pptx
thegrowingimportanceofdatacleaning-211202141902.pptx
Chapter5.ppt
database.ppt
ms-sql-server-150223140402-conversion-gate02.pptx
lecture-sql.ppt

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
A Presentation on Artificial Intelligence
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Machine learning based COVID-19 study performance prediction
The AUB Centre for AI in Media Proposal.docx
Digital-Transformation-Roadmap-for-Companies.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Building Integrated photovoltaic BIPV_UPV.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectral efficient network and resource selection model in 5G networks
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Unlocking AI with Model Context Protocol (MCP)
A Presentation on Artificial Intelligence
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
Machine learning based COVID-19 study performance prediction

stored.ppt

  • 1. Stored Procedure Language Stored Procedure Overview •Stored Procedure is a function in a shared library accessible to the database server •can also write stored procedures using languages such as C or Java •Advantages of stored procedure : Reduced network traffic •The more SQL statements that are grouped together for execution, the larger the savings in network traffic
  • 4. Writing Stored Procedures • Allows local variables, loops, procedures, examinationof one tuple ar a time. Rough Form DECLARE Declarations BEGIN Executable statements END @  The DECLARE portion is optional  Alternate terminating character in DB2 CLP scripts: ('@'), needed for ending and running.
  • 5. Simplest Form : Sequence of Modifications Employee( name , ssn, salary) BEGIN INSERT INTO EMPLOYEE VALUES (‘Sharad’, 123, 234); DELETE FROM EMPLOYEE WHERE ssn = 234; END @ To process the DB2 CLP script from the command line: db2 -tdterm-char -vf script-name Writing Stored Procedures  Tasks performed by the client application  Tasks performed by the stored procedure, when invoked  The CALL statement  Explicit parameter to be defined :  IN: Passes a value to the stored procedure from the client application  OUT: Stores a value that is passed to the client application when the stored procedure terminates.  INOUT : Passes a value to the stored procedure from the client application, and returns a value to the Client application when the stored procedure terminates
  • 6. CREATE OR REPLACE PROCEDURE <name> (<arglist>) AS <declarations> BEGIN <procedure statements> END @ EXAMPLE: CREATE PROCEDURE UPDATE_SALARY_1 (1) (IN EMPLOYEE_NUMBER CHAR(6), (2) IN RATE INTEGER) (2) LANGUAGE SQL (3) BEGIN UPDATE EMPLOYEE (4) SET SALARY = SALARY * (1.0 * RATE / 100.0 ) WHERE SSN = EMPLOYEE_NUMBER; END 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.
  • 7. Some Valid SQL Procedure Body Statements  CASE statement  FOR statement  GOTO statement  IF statement  ITERATE statement  RETURN statement  WHILE statement
  • 8. • Invoking Procedures Can invoke Stored procedure stored at the location of the database by using the SQL CALL statement • Nested SQL Procedures: To call a target SQL procedure from within a caller SQL procedure, simply include a CALL statement with the appropriate number and types of parameters in your caller. CREATE PROCEDURE NEST_SALES(OUT budget DECIMAL(11,2)) LANGUAGE SQL BEGIN DECLARE total INTEGER DEFAULT 0; SET total = 6; CALL SALES_TARGET(total); SET budget = total * 10000; END
  • 9. CONDITIONAL STATEMENTS: IF <condition> THEN <statement(s)> ELSE <statement(s)> END IF; Loops LOOP …… EXIT WHEN <condition> …… END LOOP;
  • 10. 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 @
  • 11. 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'; -- CAST salary as DOUBLE because SQL procedures do not support DECIMAL DECLARE C1 CURSOR FOR SELECT id, CAST(salary AS DOUBLE), years FROM staff; DECLARE CONTINUE HANDLER FOR not_found SET at_end = 1;
  • 12. OPEN C1; FETCH C1 INTO v_id, v_salary, v_years; WHILE at_end = 0 DO IF (v_salary < 2000 * v_years) THEN UPDATE staff SET salary = 2150 * v_years WHERE id = v_id; ELSEIF (v_salary < 5000 * v_years) THEN IF (v_salary < 3000 * v_years) THEN UPDATE staff SET salary = 3000 * v_years WHERE id = v_id; ELSE UPDATE staff SET salary = v_salary * 1.10 WHERE id = v_id; END IF; ELSE UPDATE staff SET job = 'PREZ' WHERE id = v_id; END IF; FETCH C1 INTO v_id, v_salary, v_years; END WHILE; CLOSE C1; END