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.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

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.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
Introduction to PL/SQL
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
Bordoloi and Bock PL/SQL : INTRODUCTION. Bordoloi and BockPL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational.
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.
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.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language.
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.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
11 Copyright س Oracle Corporation, All rights reserved. ® Overview of PL/SQL.
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.
PL/SQL : INTRODUCTION. PL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database language. With PL/SQL, you.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
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.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced 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.
Database Application Development using PL/SQL Programming.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Stored Procedures. Definition a stored procedure is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database.
Using Oracle-Supplied Packages. 2 home back first prev next last What Will I Learn? Describe two common uses for the DBMS_OUTPUT server-supplied package.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
DATABASE PROGRAMS CS 260 Database Systems. Overview  Introduction  Anonymous blocks  Oracle’s PL/SQL language basics  Conditions and loops  Cursors.
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.
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.
Declaring PL/SQL Variables
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Creating Stored Procedures and Functions
Declaring Variables Schedule: Timing Topic 45 minutes Lecture
Difference between Oracle PL/SQL and MySQL
Fundamentals of PL/SQL part 1 (Basics)
PL/SQL.
UNIT - V STORED PROCEDURE.
Introduction to PL/SQL
PL/SQL Scripting in Oracle:
Sa0951a PL/SQL 1: Introduction
PL/SQL week10.
PL/SQL Declaring Variables.
MATERI PL/SQL Procedures Functions Packages Database Triggers
Presentation transcript:

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 query statements of SQL are included within procedural units of code. –PL/SQL offers modern software engineering features such as data encapsulation, exception handling, information hiding, and object orientation.

PL/SQL Blocks Anonymous Block : Unnamed block of PL/SQL. These do not accept any parameters. Procedure : A series of statements accepting and/or returning zero or more variables through parameters. Function : A series of statements accepting zero or more variables that returns one value using an explicit RETURN statement. Package : A collection of procedures and functions that has two parts, a specification listing available procedures and functions and their parameters, and a body that contains the actual code for the procedures and functions. Trigger : A series of PL/SQL statements attached to a database table that execute whenever a triggering event (select, update, insert, delete) occurs.

Benefits of PL/SQL Integration Improved Performance Modularize Program Development Portability Identifiers can be used Procedural Language Control Structures Exception/Error handling

PL/SQL improves performance Application Other DBMSs Application Oracle with PL/SQL SQL SQL SQL SQL SQLIF...THENSQLELSESQL END IF; SQL

PL/SQL Block Structure DECLARE BEGIN EXCEPTION END;

DECLARE --optional BEGIN --mandatory EXCEPTION --optional END; Variables, cursors, user-defined exceptions SQL statements PL/SQL statements Actions to perform when errors occur

Executing Statements and PL/SQL Blocks from SQL*Plus Place a semicolon (;) at the end of a SQL statement or PL/SQL control statement. Use a slash (/) to run the anonymous PL/SQL block in the SQL*Plus buffer. When the block is executed successfully, without unhandled errors or compile errors, the message output should be as follows: PL/SQL procedure successfully completed. Place a period (.) to close a SQL*Plus buffer. A PL/SQL block is treated as one continuous statement in the buffer, and the semicolons within the block do not close or run the buffer.

PL/SQL statements and semicolon In PL/SQL, an error is called an exception. Section keywords DECLARE, BEGIN, and EXCEPTION are not followed by semicolons. requireEND and all other PL/SQL statements require a semicolon to terminate the statement. You can string statements together on the same line, but this method is not recommended for clarity or editing.

Use of Variables With PL/SQL you can declare variables and then use them in SQL and procedural statements anywhere an expression can be used. –Temporary storage of data –Manipulation of stored values –Reusability. –Ease of maintenance

Handling Variables in PL/SQL –Declare and initialize variables in the declaration section. –Assign new values to variables in the executable section. –Pass values into PL/SQL blocks through parameters.(will be covered later on when we talk about procedures and functions) –View results through output variables. –Remember: Uninitialized variables contain “NULL” value

identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Declare v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_genderVARCHAR2(6) := ' FEMALE ' ; v_cityVARCHAR2(10); c_commCONSTANT NUMBER := 1400; Declare v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_genderVARCHAR2(6) := ' FEMALE ' ; v_cityVARCHAR2(10); c_commCONSTANT NUMBER := 1400; Declaration of PL/SQL variables Syntax of declaration Example declarations You cannot assign NULL to v_deptno. Must be initialized The value of c_comm cannot be changed. Must be initialized at declaration

Assigning values to variables v_ename := ‘Ali'; v_hiredate := '31-DEC-2012'; identifier := expr; Set the employee name to “Ali” Set v_hiredate to a date Syntax of assignement

DBMS_OUTPUT.PUT_LINE Put_line is a procedure included in the Oracle-supplied package DBMS_OUTPUT Used for displaying data from a PL/SQL block on the screen Must be enabled in SQL*Plus with SET SERVEROUTPUT ON

Example Anonymous Blocks BEGIN DBMS_OUTPUT.PUT_LINE(‘Hello world’); END; BEGIN DBMS_OUTPUT.PUT_LINE(‘Today is’ || SYSDATE ); END; Simply prints “Hello world” on screen Put_line accepts a single string as parameter. Use concatenation operator if you have more than one string to display

Example Anonymous Blocks DECLARE v_name varchar2(20); BEGIN v_name := ‘Ali’; DBMS_OUTPUT.PUT_LINE(Hello || v_name); END; DECLARE v_name varchar2(20) := ‘Ali’; BEGIN DBMS_OUTPUT.PUT_LINE(Hello || v_name); END; Variable Initialization Variable declaration v_name varchar2(20) DEFAULT ‘Ali’; Task: Write an anonymous block to print “Hello Ali” on screen. Declare name as a variable Variable declared and initialized at declaration

Example Anonymous Blocks Read the name of user from keyboard and print “Hello ” on screen. DECLARE v_name varchar2(20); BEGIN v_name := &your_name; DBMS_OUTPUT.PUT_LINE(‘Hello ’ || v_name); END; Enter value for your_name: ‘Veli’ Old4: v_name := &your_name New4: v_name := ‘Veli’ Hello Veli Output Substitution Variable: The value you enter from keyboard replaces the substitution variable Keyboard entry

Controlling Flow of Events You can change the logical flow of statements using conditional IF statements and loop control structures. Conditional IF statements: –IF-THEN-END IF –IF-THEN-ELSE-END IF –IF-THEN-ELSIF-ELSE-END IF

IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statements;] END IF; IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statements;] END IF; IF v_dept_name = ‘ITEC' THEN v_dept_no := 35; END IF; IF v_dept_name = ‘ITEC' THEN v_dept_no := 35; END IF; Syntax Example: If v_dept_name is ‘ITEC’ change v_dept_no to 35. IF STATEMENT

Example Anonymous Block Task: Write an anonymous block that will print ‘weekend’ if the system date is ‘Sunday’ or ‘Saturday’. DECLARE v_current_date date := SYSDATE; BEGIN IF to_char(v_current_date, ‘d’) in (1,7) THEN dbms_output.put_line(‘weekend’); END IF; END;

Example Anonymous Block Task: Write an anonymous block that will print ‘weekend’ if the system date is ‘Sunday’ or ‘Saturday’ and ‘weekday’ otherwise. BEGIN IF to_char(sysdate, ‘d’) in (1,7) THEN dbms_output.put_line(‘weekend’); ELSE dbms_output.put_line(‘weekday’); END IF; END;

Example Anonymous Block Task: Write an anonymous block that will print weekend if the system date is ‘Sunday’ or ‘Saturday’, we have class if it is Tuesday or Thursday and no class otherwise. BEGIN IF to_char(sysdate, ‘d’) in (1,7) THEN dbms_output.put_line(‘week end’); ELSIF to_char(sysdate,’d’) in (3,5) THEN dbms_output.put_line(‘we have class’); ELSE dbms_output.put_line(‘no class’); END IF; END;

Summary We learned the overall format of a PL/SQL anonymous block. If statement is covered. Next lesson we will learn about loops.