PL/SQL (Embedded SQL) Introduction Benefits Basic Constructs

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Introduction to PL/SQL – Lecture 6.
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Using Oracle PL/SQL PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural.
PL/SQL Agenda: Basic PL/SQL block structure
Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.
Objectives Why PL-SQL ? Language features
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Cursor and Exception Handling By Nidhi Bhatnagar.
Stored Procedures Functions Packages
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
1 JumpStart PL/SQL Wang Hao. 2 Outline PL/SQL Programming SQL*Plus and SQL commands.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Program with PL/SQL. Interacting with the Oracle Server.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
PL/SQL Cursors Session - II. Attributes Attributes %TYPE %ROWTYPE % Found % NotFound % RowCount % IsOPen %TYPE %ROWTYPE % Found % NotFound % RowCount.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
1 CursorsCursors. 2 SQL Cursor A cursor is a private SQL work area. A cursor is a private SQL work area. There are two types of cursors: There are two.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
PL/SQL Procedural Language / Structured Query Language.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Database Application Development using PL/SQL Programming.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Implementing The Middle Tier These slides.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
DATABASE PROGRAMS CS 260 Database Systems. Overview  Introduction  Anonymous blocks  Oracle’s PL/SQL language basics  Conditions and loops  Cursors.
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
Interacting with the Oracle8 Server
Interacting with the Oracle Server
Difference between Oracle PL/SQL and MySQL
Interacting with the Oracle Server
PL/SQL.
Writing Control Structures
SQL PL/SQL Presented by: Dr. Samir Tartir
Interacting with the Oracle Server
Introduction to PL/SQL Procedures & Cursors
Cursors.
PL/SQL week10.
PL/SQL Declaring Variables.
Stored Procedure Language
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

PL/SQL (Embedded SQL) Introduction Benefits Basic Constructs Anonymous blocks Procedures Functions Packages Triggers Cursors Dynamic SQL

Introduction Embedded SQL (PL/SQL, JAVA/ VB & DB) Database Server Level Programming (PL/SQL, Transact-SQL, IBM DB2-Cobol, ProC, ProCobol) Database Client Programming Developer 9i, JDeveloper 9i, Java (J2EE), VB, .Net

- Benefits More powerful than pure SQL because it combines the power of SQL and Iteration (loops) Selection (Ifs) Cursors Block Structures Stored Procedures etc.

- Basic Constructs Basic Structure Running a program Variables SELECT INTO Comments IFs LOOPs Output

-- Basic Structure DECLARE BEGIN EXCEPTION END;

- OUTPUT … SET SERVEROUTPUT ON; BEGIN DBMS_OUTPUT.PUT_LINE('This is my fist program'); END; / Before executing code that contains DBMS_OUTPUT.PUT_LINE, must run (in a current session) at SQL prompt: set serveroutput on

--- Basic Structure: Example DECLARE v_id INTEGER; v_empno NUMBER; BEGIN v_id := 1234567; SELECT EMPNO INTO V_EMPNO FROM EMP WHERE empno = v_id; DBMS_OUTPUT.PUT_LINE('Value is '||v_empno); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No record exists'); END; /

--- Basic Structure: Example DECLARE v_id INTEGER; BEGIN v_id := 1234567; DELETE FROM EMP WHERE id = v_id; END; /

-- Running a Program DECLARE BEGIN EXCEPTION END; /

-- Variables Common Data Types Declaration NUMBER DATE INTEGER VARCHAR2 CHAR BOOLEAN Declaration V_salary NUMBER(9,2); V_id INTEGER; V_dob DATE; V_name VARCHAR2(35); V_gender CHAR; V_salary emp.salary%TYPE;

-- SELECT INTO DECLARE v_job emp.job%TYPE; v_sal emp.sal%TYPE; v_empno emp.empno%TYPE; BEGIN v_empno := 1234567; SELECT job, sal INTO v_job,v_sal FROM emp WHERE empno = v_empno; END; /

-- Another Example declare v_ename emp.ename%TYPE; v_sal emp.sal%TYPE; begin select ename, sal into v_ename, v_sal from emp where empno = '7844'; DBMS_OUTPUT.PUT_LINE('solution is:'||v_ename||'---'||v_sal); exception When others then DBMS_OUTPUT.PUT_LINE('no record'); end; / Before executing code that contains DBMS_OUTPUT.PUT_LINE, must run at SQL prompt: set serveroutput on

-- Comments DECLARE /* This block will do so and so and so. */ v_job emp.job%TYPE; v_sal emp.sal%TYPE; v_empno emp.empno%TYPE; BEGIN v_empno := 1234567; -- The use of this sentence is so and so. -- The following SELECT INTO statement will do so and so. SELECT job, sal INTO v_job,v_sal FROM emp WHERE empno = v_empno; END; /

-- IFs IF – END IF IF – ELSE – END IF IF – ELSIF – ELSE – END IF

--- IF – END IF DECLARE … BEGIN v_commison := 7500; IF v_dept = 10 THEN v_commision := 5000; END IF; END; /

--- IF – ELSE – END IF DECLARE … BEGIN IF v_dept = 10 THEN v_commision := 5000; ELSE v_commision := 7500; END IF; END; /

--- IF – ELSIF – ELSE – END IF DECLARE … BEGIN IF v_dept = 10 THEN v_commision := 5000; ELSIF v_dept = 20 THEN v_commison := 5500; ELSIF v_dept = 30 THEN v_commison := 6200; ELSE v_commision := 7500; END IF; END; /

-- LOOPs LOOP – EXIT WHEN – END LOOP FOR – LOOP – END LOOP WHILE – LOOP – END LOOP

--- LOOP – EXIT WHEN – END LOOP DECLARE … v_deptno dept.deptno%TYPE := 50; v_counter integer := 1; BEGIN LOOP INSERT INTO dept(deptno) VALUES(v_deptno); v_counter := v_counter + 1; v_deptno := v_deptno + 10; EXIT WHEN v_counter > 5; END LOOP; END; /

--- FOR – LOOP - END LOOP DECLARE … v_deptno dept.deptno%TYPE := 50; v_counter integer; BEGIN FOR v_counter IN 1..5 LOOP INSERT INTO dept(deptno) VALUES(v_deptno); v_deptno := v_deptno + 10; END LOOP; END; /

--- WHILE – LOOP - END LOOP DECLARE … v_deptno dept.deptno%TYPE := 50; v_counter integer; BEGIN v_counter := 1; WHILE v_counter <= 5 LOOP INSERT INTO dept(deptno) VALUES(v_deptno); v_deptno := v_deptno + 10; END LOOP; END; /

… - OUTPUT SET SERVEROUTPUT ON; DECLARE v_sum_sal emp.sal%TYPE; v_deptno emp.deptno%TYPE := 10; BEGIN SELECT SUM(sal) INTO v_sum_sal FROM emp WHERE deptno = v_deptno; DBMS_OUTPUT.PUT_LINE('The sum is ‘ || TO_CHAR(v_sum_sal)); END; /

- Anonymous Block DECLARE v_id INTEGER; BEGIN v_id := 1234567; DELETE FROM EMP WHERE id = v_id; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No record exists'); END; /

-- Nesting Anonymous Blocks

Exceptions Examples are NO_DATA_FOUND OTHERS To display details of oracle standard error message EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(‘Error detail is: ‘||SQLERRM)

- Procedure Is a block with a name The DECLARE key word is not used Parameters can be IN OUT IN OUT Is stored (USER_SOURCE)

-- Creating or Replacing a Procedure … CREATE OR REPLACE PROCEDURE pname( ) IS BEGIN EXCEPTION END; /

… -- Creating or Replacing a Procedure SET SERVEROUTPUT ON; CREATE OR REPLACE PROCEDURE proc_test(p_empno IN VARCHAR2) IS v_job EMP.job%TYPE; v_sal EMP.sal%TYPE; BEGIN SELECT job, sal INTO v_job,v_sal FROM emp WHERE empno = p_empno; DBMS_OUTPUT.PUT_LINE('job is '||v_job); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('ERROR...'); END; / SQL> Show errors SQL> execute proc_test(5893);

-- Invoking a Procedure DECLARE … BEGIN proc_test(’23’); END; / Or SQL> exec proc_test(‘1123’)

Another Example SQL> ed Wrote file afiedt.buf 1 create or replace procedure test_proc is 2 v_id INTEGER; 3 v_empno emp.empno%TYPE; 4 BEGIN 5 v_id := 1234567; 6 select empno into v_empno 7 FROM EMP 8 WHERE empno = v_id; 9 EXCEPTION 10 WHEN NO_DATA_FOUND THEN 11 DBMS_OUTPUT.PUT_LINE('No record exists'); 12* END; SQL> / Procedure created. SQL> exec test_proc No record exists PL/SQL procedure successfully completed. SQL> show errors (to see errors for procedures, functions)

- Function Is a block with a name Returns one value only. The DECLARE key word is not used Parameters can only be IN Is stored

-- Creating or Replacing a Function … CREATE OR REPLACE FUNCTION fname( ) RETURN datatype IS BEGIN EXECPTION END; /

… -- Creating or Replacing a Function CREATE OR REPLACE FUNCTION sum_dept_sal(p_deptno IN NUMBER) RETURN emp.sal%TYPE IS v_sum_sal emp.sal%TYPE; BEGIN SELECT SUM(sal) INTO v_sum_sal FROM emp WHERE deptno = p_deptno; RETURN v_sum_sal; END; /

-- Invoking a Function SET SERVEROUTPUT ON; DECLARE … v_sal emp.sal%TYPE; BEGIN v_sal := sum_dept_sal(10); DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_sal)); END; /

Example, passing parameters

Exercise Write down a procedure that displays all records (all columns) from EMP table. Make sure program should restricted to fix number of records of this table. Display Date and TIME portions of column that has data type DATE. Write down a procedure that can insert large number of records entered at the time of its execution. You can use EMP table and appending counter with some columns’ values

- Packages Package Invoking Package subprogram Package Specification Package Body Invoking Package subprogram

-- Package Specification CREATE OR REPLACE PACKAGE emp_info is v_count INTEGER; PROCEDURE insert_record( p_empno IN NUMBER , p_ename IN VARCHAR2 , p_job IN VARCHAR2 , p_sal IN NUMBER , p_comm IN NUMBER , p_deptno IN VARCHAR2); PROCEDURE delete_record(p_empno IN NUMBER); FUNCTION sum_dept_sal( p_deptno IN dept.deptno%TYPE) RETURN is dept.sal%TYPE; END emp_info; /

-- Package Body CREATE OR REPLACE PACKAGE BODY emp_info is` PROCEDURE insert_record(p_empno IN NUMBER,p_ename IN VARCHAR2, p_job IN VARCHAR2,p_sal IN NUMBER, p_comm IN NUMBER,p_deptno IN VARCHAR2) IS BEGIN INSERT INTO EMP(empno,ename,job,sal,comm,deptno) VALUES(p_empno,p_ename,p_job,p_sal,p_comm,p_deptno); END insert_record; PROCEDURE delete_record(p_empno IN NUMBER) IS DELETE FROM EMP WHERE empno=p_empno; END delete_record; FUNCTION sum_dept_sal(p_deptno IN NUMBER) RETURN emp.sal%TYPE IS v_sum_sal emp.sal%TYPE; SELECT SUM(sal) INTO v_sum_sal FROM emp WHERE deptno = p_deptno; RETURN v_sum_sal; END; END emp_info; --end of package body /

-- Invoking Package Subprogram DECLARE v_sum_sal emp.sal%TYPE; BEGIN … v_sum_sal := emp_info.sum_dept_sal(10); emp_info.delete_record(1234567); END; / Or SQL> exec emp_info.insert_record(…,…)

- Triggers Is a stored subprogram associated with a table. Are fired by an event Are mainly used for Security Enforce complex integrity constraint Prevent invalid transaction Event logging

-- Creating or Replacing Triggers CREATE OR REPLACE TRIGGER del_emp( p_empno emp.empno%TYPE) BEFORE DELETE ON emp FOR EACH ROW BEGIN INSERT INTO emp_audit VALUES(p_empno, USER, sysdate); END; /

… -- Trigger with function usage Example

… --Exercise Write a trigger for the following: When a record is added or deleted from an employee table, DEPT.NoOfEmp column gets updated accordingly to number of employees in EMP table corresponding to department number.

- Cursors … Is a pointer to a row. Its is mainly used with tables which return more than one row. It is handled implicitly and explicitly.

… - Cusrors … CURSOR c_emp IS SELECT empno, ename, job FROM emp WHERE deptno = 20; 7369 Smith Clerk 7566 Jones Manager current row 7788 SCOTT Analyst 7876 Adams Clerk 7906 FORD Analyst

… - Cursors DECLARE CURSOR c_emp IS SELECT empno, ename, job FROM emp WHERE deptno = 20; BEGIN FOR v_c IN c_emp LOOP … DBMS_OUTPUT.PUT_LINE(v_c.ename); END LOOP; end; /

- Example Given a table with first three columns are composite keys. Write a PL/SQL program using cursor to update cat_template1 with LONG column cat_template. Assuming this tables contains more than 40,000 records.

… - Example’s solution

Dynamic SQL begin execute immediate 'create table tt(id number(3))'; end; /