PL/SQL Procedural Language / Structured Query Language (Source CaoSys)

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
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.
Chapter 10 Introduction to Arrays
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Guide To UNIX Using Linux Third Edition
Introduction to PL/SQL Lecture 0 – Self Study Akhtar Ali.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
INTERNET APPLICATION DEVELOPMENT For More visit:
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
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.
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
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 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.
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 Procedural Language / Structured Query Language.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
5 1 Data Files CGI/Perl Programming By Diane Zak.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
ITEC 224 Database Programming PL/SQL Lab Cursors.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
CREATING STORED PROCEDURES AND FUNCTIONS. Objectives After completing this lecture, you should be able to do the following: Differentiate between anonymous.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Files Tutor: You will need ….
implicit and an explicit cursor
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Using Data Structures in Embedded Programs.
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.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Files A collection of related data treated as a unit. Two types Text
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
COMP 430 Intro. to Database Systems
A Guide to SQL, Seventh Edition
SQL and SQL*Plus Interaction
PL/SQL.
UNIT - V STORED PROCEDURE.
Using SQL*Plus.
PL/SQL Package Week 8.
Working with Composite Datatypes
Topics Introduction to File Input and Output
Using files Taken from notes by Dr. Neil Moore
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
PRACTICE OVERVIEW PL/SQL Part - 1.
Chapter 8 Advanced SQL.
Using SQL*Plus.
PL/SQL Declaring Variables.
Topics Introduction to File Input and Output
Files Chapter 8.
Presentation transcript:

PL/SQL Procedural Language / Structured Query Language (Source CaoSys)

Overview PL/SQL Records and Tables Host File Access with UTL_FILE

Records To create a record manually that contains employee information you might use the following:- TYPE t_emp_record IS RECORD ( enumber NUMBER, firstname VARCHAR2(20), lastname VARCHAR2(20)); At this point, no PL/SQL object exists, just the new datatype, so, we need to declare a variable of this type:- l_employee t_emp_record;

Records (Continued) This is no different to declaring a variable of any of the standard types. You can now use the new l_employee record with the dot notation to get at its elements, for example:- l_employee.enumber := 101; l_employee.firstname := ‘John'; l_employee.lastname := ‘Smith'; You can assign one record to another so long as all the elements in both records are exactly the same, for example:-

Records (Continued) You can assign one record to another so long as all the elements in both records are exactly the same, for example:- DECLARE TYPE t_record IS RECORD ( col1 NUMBER, col2 VARCHAR2(10) ); l_record1 t_record; l_record2 t_record; BEGIN l_record1.col1 := 10; l_record1.col2 := 'Test'; l_record2 := l_record1; END;

Records (Continued) Records can also be used as arguments in procedures and functions, for example:- CREATE OR REPLACE PROCEDURE print_dept (p_dept_rec dept%ROWTYPE) IS BEGIN DBMS_OUTPUT.put_line(p_dept_rec.deptno); END;

Records (Continued) The procedure could be invoked with:- DECLARE l_rec dept%ROWTYPE; BEGIN SELECT * INTO l_rec FROM dept WHERE deptno = 10; print_dept(l_rec); END;

Tables A PL/SQL table is very similar to an array found in most third generation languages. Before a table can be used, you need to declare a new datatype that is of type table, you do this with the TYPE statement in a similar way as you create records. The syntax of TYPE in this case is:- TYPE table-name IS TABLE OF type INDEX BY BINARY_INTEGER;

Tables (Continued) Where table-name can be any valid identifier and type is any valid datatype, including any new datatypes you have created, such as a record. So, to declare a table to hold the names of employees you might:- TYPE t_names IS TABLE OF emp.ename%TYPE INDEX BY BINARY_INTEGER;

Tables (Continued) Once your new table type is declared, you must declare a variable of the new type, for example:- names_tab t_names; The above code will create a new variable called names_tab of type t_names. You can now use the table names_tab. You access individual rows on a PL/SQL table by using a table index (reference/subscript), similar to an array subscript. The index should appear immediately after the table name, for example, to set the elements of record one in the names_tab table:- names_tab(1).empno := 10; names_tab(1).ename := ‘John';

Tables (Continued) Here is another example of a less complex table, this time it is a table of a scalar datatype:- DECLARE TYPE t_names IS TABLE OF VARCHAR2(10) INDEX BY BINARY_INTEGER; names_tab t_names; BEGIN names_tab(-10) := ‘John'; names_tab(0) := ‘Mary'; names_tab(250) := ‘Andrew'; END;

Tables (Continued) Memory has only been allocated for 3 rows, this is very much unlike arrays. To set the value of the 250’th row in an array, all elements preceding it must exist. Nor can an array have a negative subscript. PL/SQL tables grow dynamically in size as you create rows, very much like a database table.

Tables (Continued) Memory has only been allocated for 3 rows, this is very much unlike arrays. To set the value of the 250’th row in an array, all elements preceding it must exist. Nor can an array have a negative subscript. PL/SQL tables grow dynamically in size as you create rows, very much like a database table. Points of interest: Can not reference undeclared row, as it will result in a exception. A table object has a number of attributes which we can use to interact with it e.g. Count, last, exists etc.

Tables (Continued) COUNT: This will return the number of rows in the table. DECLARE TYPE t_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; number_tab t_table; l_rows NUMBER; BEGIN number_tab(1) := 10; number_tab(10) := 20; number_tab(87) := 5; number_tab(2500) := 100; l_rows := number_tab.COUNT; END; In the above code, l_rows will be set to 4.

Tables (Continued) DELETE: This is used to remove rows from a table. DECLARE TYPE t_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; number_tab t_table; BEGIN number_tab(1) := 10; number_tab(10) := 20; number_tab(87) := 5; number_tab(2500) := 100; number_tab.DELETE(87); END; DELETE can be used in three ways:- table.DELETE; will remove all rows table.DELETE(x); remove row i. table.DELETE(x,y); remove rows i through y.

Tables (Continued) EXISTS: This is used to check whether a specified row exists or not. DECLARE TYPE t_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; number_tab t_table; BEGIN number_tab(1) := 10; number_tab(10) := 20; number_tab(87) := 5; number_tab(2500) := 100; IF number_tab.EXISTS(10) THEN DBMS_OUTPUT.put_line('Row 10 Found'); END IF; IF NOT number_tab.EXISTS(100) THEN DBMS_OUTPUT.put_line('Row 100 not found'); END IF; END;

Tables (Continued) FIRST and LAST: These are used to find the index of the first and last rows in a table. DECLARE TYPE t_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; number_tab t_table; l_first BINARY_INTEGER; l_last BINARY_INTEGER; BEGIN number_tab(1) := 10; number_tab(10) := 20; number_tab(87) := 5; number_tab(2500) := 100; l_first := number_tab.FIRST; l_last := number_tab.LAST; END; In the above code, l_first wil be set to 1 and l_last will be set to 2500.

Records and Tables Review PL/SQL are generally very much under-used. They are very powerful constructs and greatly enhance the functionality of PL/SQL. Many programs need to have some kind of temporary storage area, normally used to hold intermediate data which needs to be processed in some way, a great deal of developers create this temporary storage using database tables, while this offers some advantages, such as the ability to restart a failed process from where it last was, PL/SQL tables offer advantages too; such a vastly increased performance, PL/SQL tables are much faster to work with than database tables, as everything is done in memory. You also have the advantage of not having to create and maintain a temporary table. The only real disadvantage is slightly more complex code.

Host File Access UTL_FILE

Host File Access (Cont) Reading from and writing to host files is a common task for PL/SQL. PL/SQL itself does not directly support this kind of functionality, but it can be done using an Oracle supplied package, UTL_FILE. UTL_FILE is a server side package for writing to host files on the server, there is another package, TEXT_IO.

Host File Access (Cont) UTL_FILE has the following functions and procedures: FOPEN Used to open a file FCLOSE Used to closed a file FCLOSE_ALL Close all open files IS_OPEN Check if a file is open FFLUSH Output from UTL_FILE buffered, this procedure is used to ensure the buffer has been written to the file. PUT Write to file PUT_LINE Write to file NEW_LINE Write to file PUTF Write to file GET_LINE Read from a file

Host File Access (Cont) FOPEN: Use this function to open a file. FUNCTION fopen( path IN VARCHAR2, filename IN VARCHAR2, mode IN VARCHAR2) RETURN FILE_TYPE; PATH the directory containing the file FILENAME the actual filename MODE the Open Mode, this can be one of: 'r' : Read from file 'w' : Write to file 'a' : Append to file

Host File Access (Cont) The return type is the File Handle which is used to reference the open file is other functions. It’s type is UTL_FILE.file_type. DECLARE l_handle UTL_FILE.file_type; BEGIN l_handle := UTL_FILE.fopen( '/u01/app', 'my_file.dat', 'r');... The above code will open the file /u01/app/my_file.dat for reading.

Host File Access (Cont) PUT & PUT_LINE: This procedure is used to write data to a file. UTL_FILE.put(handle,buffer); Where handle is the handle of an already open file and buffer the string to be written. PUT does not append a newline character to the buffer; you should use PUT_LINE or NEW_LINE for this. PUT_LINE is almost the same as PUT except that it automatically add’s a newline character to the end of buffer. DECLARE l_handle UTL_FILE.file_type; BEGIN l_handle := UTL_FILE.fopen( '/u01/app', 'my_file.dat', 'w'); UTL_FILE.put(l_handle,'This line is written'); UTL_FILE.fclose(l_handle); END;

Host File Access (Cont) PUTF: This procedure is used to write data to a file. UTL_FILE.putf(handle,format,arg1,arg2,arg3,arg4,arg5); Where handle is the handle of an already open file. The parameters arg1 to arg5 are optional and are string that are written to the file in a format specified by format. format is any string of text which can contain special codes used to print the strings in arg1 to arg5. The %s code is replaced in the string with is corresponding arg parameter. You can also use the code \n to add a newline character. It is similar to C’s printf function. DECLARE l_handle UTL_FILE.file_type; BEGIN l_handle := UTL_FILE.fopen( '/u01/app', 'my_file.dat', 'w'); UTL_FILE.putf(l_handle,'arg1=%s\n',’John'); UTL_FILE.fclose(l_handle); END;

Host File Access (Example) Below is a procedure that can be used to dump out the contents of the dept table to a file. CREATE OR REPLACE PROCEDURE dump_dept IS CURSOR dept_cur IS SELECT deptno, dname, loc FROM dept; l_handle UTL_FILE.file_type; l_path VARCHAR2(50) := '/u01/app'; l_filename VARCHAR2(20) := 'dept.dat'; l_record VARCHAR2(200);

Host File Access (Example) BEGIN -- Open file l_handle := UTL_FILE.fopen( l_path, l_filename, 'w'); -- Get all dept rows FOR r_dept IN dept_cur LOOP l_record := TO_CHAR(r_dept.deptno)||'|'|| r_dept.dname||'|'|| TO_CHAR(r_dept.loc); -- Write row to file UTL_FILE.put_line(l_handle,l_record); END LOOP; UTL_FILE.fflush(l_handle); UTL_FILE.fclose(l_handle); EXCEPTION WHEN UTL_FILE.invalid_path THEN DBMS_OUTPUT.put_line('Invalid Path'); WHEN UTL_FILE.write_error THEN DBMS_OUTPUT.put_line('Write Error'); END;