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.

Slides:



Advertisements
Similar presentations
Owners Roles DB Objects Grant Revoke Grant Revoke Grant Revoke (*) with admin option Il ruolo …. È un set di system & object privileges Non appartiene.
Advertisements

PL/SQL.
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
H.Melikian1 PLSQL Dr.Hayk Melikyan Departmen of Mathematics and CS
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
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.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
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.
Chapter 4B: More Advanced PL/SQL Programming
PL/SQL Agenda: Basic PL/SQL block structure
Oracle PL/SQL Eyad Husni Elshami. Why PL/SQL Block Structures: – PL/SQL consists of blocks of code, which can be nested within each other. Each block.
Objectives Why PL-SQL ? Language features
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
SQL Training Procedures & Functions. Confidential & Proprietary Copyright © 2009 Cardinal Directions, Inc. DB Procedures & Functions Procedures and Functions.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
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.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
Advanced SQL: Cursors & Stored Procedures
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
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.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Implementing The Middle Tier These slides.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
implicit and an explicit cursor
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
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.
Database An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
PL/pgSQL
SQL and SQL*Plus Interaction
Difference between Oracle PL/SQL and MySQL
PL/SQL MULTIPLE CHOICE QUESTION.
SQL PL/SQL Presented by: Dr. Samir Tartir
Interacting with the Oracle Server
Working with Composite Datatypes
PL/SQL week10.
Using SQL*Plus.
Information Management
Chapter 8 Advanced SQL Pearson Education © 2009.
Stored Procedure Language
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Database Programming Using Oracle 11g
Presentation transcript:

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 language that is more powerful than SQL. The basic unit in PL/SQL is a block. All PL/SQL programs are made up of blocks

block structure DECLARE /* Declarative section: variables, types, and local subprograms. */ BEGIN /* Executable section: procedural and SQL statements go here. */ /* This is the only section of the block that is required. */ EXCEPTION /* Exception handling section: error handling statements go here. */ END;

Execute a PL/SQL To execute a PL/SQL program must follow the program text itself by –A line with a single dot ("."), and then –A line with run; with Oracle SQL programs, we can invoke a PL/SQL program by typing it in sqlplus

Variables and Types Type –One of the types used by SQL for database columns –A generic type used in PL/SQL such as NUMBER –Declared to be the same as the type of some database column E.G. DECLARE price NUMBER; myName VARCHAR(20);

Variables and Types %TYPE operator DECLARE myName Emp.name%TYPE; %ROWTYPE operator DECLARE EmpTuple Emp%ROWTYPE;

Variables and Types ":=" operator e.g DECLARE a NUMBER := 3; BEGIN a := a + 1; END;. run;

Simple Programs in PL/SQL Plain SQL CREATE TABLE T1( e INTEGER, f INTEGER ); DELETE FROM T1; INSERT INTO T1 VALUES(1, 3); INSERT INTO T1 VALUES(2, 4); ef T1

Simple Programs in PL/SQL PL/SQL program DECLARE a NUMBER; b NUMBER; BEGIN SELECT e,f INTO :a,:b FROM T1 WHERE e>1; INSERT INTO T1 VALUES(:b,:a); END;. run; ef T1 42

Control Flow in PL/SQL IF statement IF THEN... ELSIF THEN ELSIF THEN... ELSE... END IF;

Control Flow in PL/SQL E.g. DECLARE a NUMBER; b NUMBER; BEGIN SELECT e,f INTO :a, :b FROM T1 WHERE e>1; IF b=1 THEN INSERT INTO T1 VALUES(:b,:a); ELSE INSERT INTO T1 VALUES(:b+10,:a+10); END IF; END;. run;

Control Flow in PL/SQL Loops : LOOP /* A list of statements. */ END LOOP; EXIT EXIT WHEN ;

E.G. DECLARE i NUMBER := 1; BEGIN LOOP INSERT INTO T1 VALUES(:i,:i); i := i+1; EXIT WHEN i>100; END LOOP; END;. run;

Control Flow in PL/SQL WHILE loop WHILE LOOP END LOOP; FOR loop FOR IN.. LOOP END LOOP;

DECLARE x NUMBER := 100; BEGIN FOR i IN LOOP IF MOD(i,2) = 0 THEN -- i is even INSERT INTO temp VALUES (i,: x, 'i is even'); ELSE INSERT INTO temp VALUES (i, :x, 'i is odd'); END IF; x := x + 100; END LOOP; COMMIT; END;. run

DECLARE acct_balance NUMBER(11,2); acct CONSTANT NUMBER(4) := 3; debit_amt CONSTANT NUMBER(5,2) := ; BEGIN SELECT bal INTO :acct_balance FROM accounts WHERE account_id = :acct FOR UPDATE OF bal; IF acct_balance >= debit_amt THEN UPDATE accounts SET bal = bal - :debit_amt WHERE account_id = :acct; ELSE INSERT INTO temp VALUES (:acct, :acct_balance, 'Insufficient funds'); -- insert account, current balance, and message END IF; COMMIT; END;

DECLARE salary emp.sal%TYPE; mgr_num emp.mgr%TYPE; last_name emp.ename%TYPE; starting_empno CONSTANT NUMBER(4) := 7902; BEGIN SELECT sal, mgr INTO :salary, :mgr_num FROM emp WHERE empno = :starting_empno; WHILE salary < 4000 LOOP SELECT sal, mgr, ename INTO :salary, :mgr_num, :last_name FROM emp WHERE empno = :mgr_num; END LOOP ; INSERT INTO temp VALUES (NULL, :salary, :last_name); COMMIT; END;

Cursors Definition CURSOR T1 Cursor IS SELECT e, f FROM T1 WHERE e < f;

1) DECLARE /* Output variables to hold the result of the query: */ 2) a T1.e%TYPE; 3) b T1.f%TYPE; /* Cursor declaration: line 4-8*/ 4) BEGIN 9) OPEN T1Cursor; 10) LOOP /* Retrieve each row of the result of the above query into PL/SQL variables: */ 11) FETCH T1Cursor INTO a, b; /* If there are no more rows to fetch, exit the loop: */ 12) EXIT WHEN T1Cursor%NOTFOUND; /* Insert the reverse tuple: */ 13) INSERT INTO T1 VALUES(b, a); 14) END LOOP; /* Free cursor used by the query. */ 15) CLOSE T1Cursor; 16) END; 17). 18) run;

DECLARE CURSOR my_cursor IS SELECT sal + NVL(comm, 0) wages, ename FROM emp; my_rec my_cursor%ROWTYPE; BEGIN OPEN my_cursor; LOOP FETCH my_cursor INTO :my_rec; EXIT WHEN my_cursor% NOTFOUND; IF my_rec.wages > 2000 THEN INSERT INTO temp VALUES (NULL, my_rec.wages, my_rec.ename); END IF; END LOOP; CLOSE my_cursor; END;

DECLARE CURSOR num1_cur IS SELECT num FROM num1_tab ORDER BY sequence; CURSOR num2_cur IS SELECT num FROM num2_tab ORDER BY sequence; num1 num1_tab.num%TYPE; num2 num2_tab.num%TYPE; pair_num NUMBER := 0; BEGIN OPEN num1_cur; OPEN num2_cur; LOOP -- loop through the two tables and get -- pairs of numbers FETCH num1_cur INTO :num1; FETCH num2_cur INTO :num2; EXIT WHEN (num1_cur%NOTFOUND) OR (num2_cur%NOTFOUND); pair_num := pair_num + 1; INSERT INTO sum_tab VALUES (pair_num, num1 + num2); END LOOP; CLOSE num1_cur; CLOSE num2_cur; END;

DECLARE CURSOR c1 is SELECT ename, empno, sal FROM emp ORDER BY sal DESC; -- start with highest-paid employee my_ename CHAR(10); my_empno NUMBER(4); my_sal NUMBER(7,2); BEGIN OPEN c1; LOOP FETCH c1 INTO :my_ename, :my_empno, :my_sal; EXIT WHEN (c1%ROWCOUNT > 5) OR (c1%NOTFOUND); INSERT INTO temp VALUES (my_sal, my_empno, my_ename); COMMIT; END LOOP; CLOSE c1; END;

DECLARE num1 data_table.n1%TYPE; -- Declare variables num2 data_table.n2%TYPE; -- to be of same type as num3 data_table.n3%TYPE; -- database columns result temp.num_col1%TYPE; CURSOR c1 IS SELECT n1, n2, n3 FROM data_table WHERE exper_num = 1; BEGIN OPEN c1; LOOP FETCH c1 INTO :num1, :num2, :num3; EXIT WHEN c1%NOTFOUND; -- the c1%NOTFOUND condition evaluates -- to TRUE when FETCH finds no more rows /* calculate and store the results */ result := num2/(num1 + num3); INSERT INTO temp VALUES (result, NULL, NULL); END LOOP; CLOSE c1; COMMIT; END;

I/O Control DBMS_OUTPUT.NEW_LINE(); DBMS_OUTPUT.PUT_LINE(); DBMS_OUTPUT.PUT();

I/O Example BEGIN /* Get Current User Name */ SELECT username INTO :l_current_user FROM USER_USERS; DBMS_OUTPUT.NEW_LINE(); DBMS_OUTPUT.PUT('Connect As '||l_current_user); DBMS_OUTPUT.PUT(' And Add Details For '||RTRIM(Empname)); DBMS_OUTPUT.PUT_LINE(' having Id '||RTRIM(empid)); INSERT INTO Employee_table VALUES(Empid, Empname, Empadd, Deptcd, Grade, SYSDATE); /* Increment Department strength */ Increment_Dept_Strength(Deptcd); END Insert_Emp_Details;

Logging In to Oracle log in to Oracle by typing: sqlplus Changing Your Password alter user <yourName identified by <newPassword;

Quitting sqlplus To leave sqlplus, type quit;

Executing SQL From a File sqlplus e.g. sqlplus

Editing Commands L lists the command buffer, and makes the last line in the buffer the "current" line L nprints line n of the command buffer, and makes line n the current line L m nprints lines m through n, and makes line n the current line

Editing Commands - cont. I enters a mode that allows you to input text following the current line; you must terminate the sequence of new lines with a pair of "returns" C /old/new replaces the text " old " by " new " in the current line A text appends " text " to the end of the current line DEL deletes the current line

Exercises 4.5 using PL/SQL