Oracle 8i Building PL/SQL Blocks. Creating PL/SQL Blocks CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Data Definition Language (DDL)
Oracle PL/SQL IV Exceptions Packages.
1 A GUIDE TO ORACLE8 CHAPTER 2: Creating and ModifyingDatabaseTables 2.
Oracle Objects Object Oriented Database Approach.
1 Chapter 2: Creating and Modifying Database Tables.
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Objectives Why PL-SQL ? Language features
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
CHAPTER 11 Large Objects. Need for Large Objects Data type to store objects that contain large amount of text, log, image, video, or audio data. Most.
Overview Classes of datatypes available in Oracle 10g – Character – Numeric – Long, Raw – Dates/Times – Large Objects (LOBs) – ROWID – Specialized 1.
Cursor and Exception Handling By Nidhi Bhatnagar.
Distributed Database Applications COSC 5050 Week Two.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Stored Procedures Functions Packages
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.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
Oracle Database Administration
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
Sizing Basics  Why Size?  When to size  Sizing issues:  Bits and Bytes  Blocks (aka pages) of Data  Different Data types  Row Size  Table Sizing.
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 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
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 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 -
PL/SQL. Introduction to PL/SQL block Declare declarations Begin executable statement Exception exception handlers End;
Database Application Development using PL/SQL Programming.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
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.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
6 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
Visual Programing SQL Overview Section 1.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Data types  CHAR (size): This data type is used to store character strings values of fixed length. The size in brackets determines the number of characters.
8 Copyright © 2004, Oracle. All rights reserved. Managing Schema Objects.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
Academic Year 2015 Autumn. MODULE CC2006NI: Data Modelling and Database Systems Academic Year 2015 Autumn.
Declaring PL/SQL Variables
PL/SQL INTRODUCTION.
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
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.
1 Part IV: Object-Relational Databases  Ch. 30: Types, Object Views, Methods  Ch. 31: Nested Tables and Varying Arrays  Ch. 32: Large Objects  Ch.
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.
7 Copyright © 2004, Oracle. All rights reserved. Managing Schema Objects.
PLSQL Cont…. Most Common Oracle Data Types VARCHAR2 –Stores variable-length character data. –Takes a required parameter that specifies a maximum length.
CIT 214 Introduction to Database Management
Creating Database Objects
C LANGUAGE MULITPLE CHOICE QUESTION SET-2
Introduction To Oracle
PL/SQL Declaring Variables Writing Executable Statements
CS 3630 Database Design and Implementation
Module 2: Creating Data Types and Tables
Lesson 7 Managing Data Creating a database with Web Matrix.
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
DATABASE MANAGEMENT SYSTEM
SQL data definition using Oracle
Database Management Systems 2
Local Variables, Global Variables and Variable Scope
PL/SQL week10.
Object-Relational Features
PL/SQL Declaring Variables.
Creating Database Objects
Database Programming Using Oracle 11g
Presentation transcript:

Oracle 8i Building PL/SQL Blocks

Creating PL/SQL Blocks CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS CURSORS PROCEDURES FUNCTIONS PACKAGES TRIGGERS

Structures of a PL/SQL block The Declaration section The Execution section The Exception section DECLARE --- BEGIN --- EXCEPTION --- END; The Declaration section The Execution section The Exception section DECLARE --- BEGIN --- EXCEPTION --- END;

Declaration Section Datatypes: NUMBER –  –NUMBER, NUMBER(PRE),NUMBER(PRE,SC) –Default precision – 38 –Scale: -84 to 127 (auto round) –Subtypes: DEC/DECIMAL/NUMERIC – fixed point FLOAT – 128BIT REAL – 64BIT INT/INTEGER/SMALLINT Datatypes: NUMBER –  –NUMBER, NUMBER(PRE),NUMBER(PRE,SC) –Default precision – 38 –Scale: -84 to 127 (auto round) –Subtypes: DEC/DECIMAL/NUMERIC – fixed point FLOAT – 128BIT REAL – 64BIT INT/INTEGER/SMALLINT

Characters CHAR – max 2000 chars (default: 1) CHARACTER – same(ANSI) VARCHAR2 – max 4000 chars NCHAR NVARCHAR LONG – Up To 2GB CHAR – max 2000 chars (default: 1) CHARACTER – same(ANSI) VARCHAR2 – max 4000 chars NCHAR NVARCHAR LONG – Up To 2GB

More datatypes LONG RAW – 2GB – binary RAW – binary – 2000 bytes CLOB – char large object(same as LONG) NCLOB – unicode BLOB – binary large object BFILE – file pointer DATE LONG RAW – 2GB – binary RAW – binary – 2000 bytes CLOB – char large object(same as LONG) NCLOB – unicode BLOB – binary large object BFILE – file pointer DATE

Variables DECLARE my_variable NUMBER(4); my_second_var NUMBER(4):=22; my_variable:=77; DECLARE my_variable NUMBER(4); my_second_var NUMBER(4):=22; my_variable:=77;

SELECT INTO DECLARE my_variable NUMBER(4); my_second_var NUMBER(4); BEGIN my_variable := 100; SELECT unit_price INTO my_second_var FROM product WHERE product_id = 50; Dbms_output.Put_line(my_variable); Dbms_output.Put_line('The unit price is: ' || my_second_var); END; DECLARE my_variable NUMBER(4); my_second_var NUMBER(4); BEGIN my_variable := 100; SELECT unit_price INTO my_second_var FROM product WHERE product_id = 50; Dbms_output.Put_line(my_variable); Dbms_output.Put_line('The unit price is: ' || my_second_var); END;

Scope of Variables DECLARE v1 NUMBER; v2 NUMBER; /******************** The new block begins.... *******************/ BEGIN v1 := 100; v2 := 200; DECLARE v3 NUMBER; BEGIN v3 := v2 + v1; END; --v3 cannot be accessed FROM here END; DECLARE v1 NUMBER; v2 NUMBER; /******************** The new block begins.... *******************/ BEGIN v1 := 100; v2 := 200; DECLARE v3 NUMBER; BEGIN v3 := v2 + v1; END; --v3 cannot be accessed FROM here END;

Constants DECLARE c_new_sal CONSTANT NUMBER (4) := 500; v_current_sal NUMBER(9,2); BEGIN SELECT salary INTO v_current_sal FROM sales_person WHERE sales_person_id = 804; Dbms_output.Put_line ('Current salary: ' || v_current_sal); v_current_sal := v_current_sal + c_new_sal; Dbms_output.Put_line ('New salary: ' || v_current_sal); END; DECLARE c_new_sal CONSTANT NUMBER (4) := 500; v_current_sal NUMBER(9,2); BEGIN SELECT salary INTO v_current_sal FROM sales_person WHERE sales_person_id = 804; Dbms_output.Put_line ('Current salary: ' || v_current_sal); v_current_sal := v_current_sal + c_new_sal; Dbms_output.Put_line ('New salary: ' || v_current_sal); END;

Records DECLARE TYPE product_type IS RECORD (f_product_id NUMBER(5), f_product_name VARCHAR2(25), f_supplier_name product.supplier_name%type, f_unit_price product.unit_price%type); product_rec product_type; DECLARE TYPE product_type IS RECORD (f_product_id NUMBER(5), f_product_name VARCHAR2(25), f_supplier_name product.supplier_name%type, f_unit_price product.unit_price%type); product_rec product_type;

ROWTYPE record_name table_name%ROWTYPE; Example: DECLARE product_rec product%ROWTYPE; record_name table_name%ROWTYPE; Example: DECLARE product_rec product%ROWTYPE;

Passing Values record_name.field_name := value; If you used %ROWTYPE: record_name.col_name := value; SELECT col1, col2 INTO record_name.field_name1, record_name.field_name2 [WHERE clause]; If you use %ROWTYPE: SELECT * INTO record_name FROM table_name [WHERE clause]; record_name.field_name := value; If you used %ROWTYPE: record_name.col_name := value; SELECT col1, col2 INTO record_name.field_name1, record_name.field_name2 [WHERE clause]; If you use %ROWTYPE: SELECT * INTO record_name FROM table_name [WHERE clause];

%type example DECLARE comp_name customer.company_name%type; cont customer.contact_name%type; BEGIN SELECT company_name, contact_name INTO comp_name,cont FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| comp_name); Dbms_output.Put_line ('contact: '||cont); END; DECLARE comp_name customer.company_name%type; cont customer.contact_name%type; BEGIN SELECT company_name, contact_name INTO comp_name,cont FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| comp_name); Dbms_output.Put_line ('contact: '||cont); END;

RECORD example DECLARE TYPE cust_type IS RECORD( comp_name customer.company_name%type, cont customer.contact_name%type); cust_rec cust_type; BEGIN SELECT company_name, contact_name INTO cust_rec FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| cust_rec.comp_name); Dbms_output.Put_line ('contact: '|| cust_rec.cont); END; DECLARE TYPE cust_type IS RECORD( comp_name customer.company_name%type, cont customer.contact_name%type); cust_rec cust_type; BEGIN SELECT company_name, contact_name INTO cust_rec FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| cust_rec.comp_name); Dbms_output.Put_line ('contact: '|| cust_rec.cont); END;

ROWTYPE DECLARE cust_rec customer%ROWTYPE; BEGIN SELECT * INTO cust_rec FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| cust_rec.company_name); Dbms_output.Put_line ('contact: '|| cust_rec.contact_name); END; DECLARE cust_rec customer%ROWTYPE; BEGIN SELECT * INTO cust_rec FROM customer WHERE customer_id = 300; Dbms_output.Put_line ('company: '|| cust_rec.company_name); Dbms_output.Put_line ('contact: '|| cust_rec.contact_name); END;

Manipulating Data DECLARE prod_rec product%ROWTYPE; BEGIN INSERT INTO product VALUES (100, 'CD', 'American Suppliers', 19.99); SELECT * INTO prod_rec FROM product WHERE product_id = 100; Dbms_output.Put_line (prod_rec.product_id); Dbms_output.Put_line (prod_rec.product_name); Dbms_output.Put_line (prod_rec.supplier_name); Dbms_output.Put_line (prod_rec.unit_price); COMMIT; END; DECLARE prod_rec product%ROWTYPE; BEGIN INSERT INTO product VALUES (100, 'CD', 'American Suppliers', 19.99); SELECT * INTO prod_rec FROM product WHERE product_id = 100; Dbms_output.Put_line (prod_rec.product_id); Dbms_output.Put_line (prod_rec.product_name); Dbms_output.Put_line (prod_rec.supplier_name); Dbms_output.Put_line (prod_rec.unit_price); COMMIT; END;