PL/SQL ONLINE TRAINING

Post on 15-Apr-2017

312 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

Transcript

PL/SQL ONLINE TRAINING

Email Id : trainings@cheyat.com Contact IND : +91-9100998433/34 USA : +1(224) 676-9882 www.cheyat.com

Introduction to Oracle Database Introduction to Oracle DatabaseRetrieve Data using the SQL SELECT StatementLearn to Restrict and Sort DataUsage of Single-Row Functions to Customize OutputInvoke Conversion Functions and Conditional ExpressionsAggregate Data Using the Group FunctionsDisplay Data From Multiple Tables Using JoinsUse Sub-queries to Solve QueriesThe SET OperatorsData Manipulation StatementsUse of DDL Statements to Create and Manage TablesControl User AccessManagement of Schema ObjectsRetrieve Data Using Sub-queriesOLAP QuieresPseudo Columna Hierarchical Queries

COURSE CONTENT

COURSE CONTENT

Introduction to PL/SQL

PL/SQL Identifiers Write Executable Statements Interaction with the Oracle Server Control Structures Write Executable Statements Composite Data Types Explicit Cursors Exception Handling Stored Procedures and Functions Create Stored Procedures Create Stored Functions Create Packages Implement Oracle-Supplied Packages in Application

Development Dynamic SQL Design Considerations for PL/SQL Code Describe Triggers Create Compound, DDL, and Event Database Triggers

WHAT IS PL/SQL

Stands for Procedural Languages extension to SQl

Is Oracle Corporations’s standard data access language for relational databases.

Seamlessly integrates Procedural constructs with QL

ABOUT PL/SQL

• Provides a block structure for executable units of code. Maintenance of code is made easier with such a well-defined structure• Provides procedural constructs such as: – Variables, constants, and types – Control structures such as conditional statements and loops – Reusable program units that are written once and executed many times

Benefits of PL/SQL

– It is portable. – You can declare identifiers. – You can program with procedural language control structures. – It can handle errors.

BENEFITS OF PL/SQL

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

PL/SQL Block Structure

 IDENTIFIERS

 IDENTIFIERS ARE USED FOR: • Naming a variable • Providing conventions for variable names – Must start with a letter – Can include letters or numbers – Can include special characters (such as dollar sign, underscore, and pound sign) – Must limit the length to 30 characters – Must not be reserved words

Variables can be used for:

• Temporary storage of data • Manipulation of stored values • Reusability

 Use of Variables

• PL/SQL variables: – Scalar – Composite – Reference – Large object (LOB) • Non-PL/SQL variables: Bind variables

Types of Variables

Variables are:

• Declared and initialized in the declarative section • Used and assigned new values in the executable section • Passed as parameters to PL/SQL subprograms • Used to hold the output of a PL/SQL subprogram

Handling Variables in PL/SQL

FUNCTIONS

A function is a named PL/SQL Block which is similar to a procedure. The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value

FUNCTION EXAMPLE

• Create function funname • Return number is • a number(10); • Begin • Select avg(sal) into a from emp;• return a; • End; • / 

CREATE TRIGGERS

• A Trigger is a PL/SQL block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. A trigger is triggered automatically when an associated DML statement is executed. 

• Insert Triggers:

BEFORE INSERT Trigger

AFTER INSERT Trigger

• Update Triggers:

BEFORE UPDATE Trigger

AFTER UPDATE Trigger

• Delete Triggers:

BEFORE DELETE Trigger

AFTER DELETE Trigger

• Drop Triggers:

Drop a Trigger

• Disable/Enable Triggers:

Disable a Trigger

Disable all Triggers on a table

Enable a Trigger

Enable all Triggers on a table

A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains information on a select statement and the rows of data accessed by it. • Cursors provide a way for your program to select multiple rows of data from the database and then to process each row individually. • There are two types of cursors in PL/SQL: • 1)IMPLICIT CURSORS • 2) Explicit cursors

CURSORS

Implicit Cursors • These are created by default when DML statements like, INSERT, UPDATE,

and DELETE statements are executed. • The set of rows returned by query is called active set. • Oracle provides few attributes called as implicit cursor attributes to check

the status of DML operations. The are as follows 1) %FOUND 2) %NOTFOUND 3) %ROWCOUNT 4) %ISOPEN. 61

Implicit Cursors

 DECLARE • n number(5); • BEGIN • UPDATE emp SET salary = salary + 1000; • IF SQL%NOTFOUND THEN• dbms_output.put_line('No sal are updated'); • ELSIF SQL%FOUND THEN• n := SQL%ROWCOUNT; • dbms_output.put_line('Sal for ' ||n || 'employees are updated'); • END IF; • END; • /

FOR E.G.

• %FOUND->The return value is TRUE, if the DML statements like INSERT, DELETE and UPDATE affect at least one row and if SELECT ….INTO statement return at least one row. • %NOTFOUND->same as above but if not found. • %ROWCOUNT ->Return the number of rows affected by the DML operations 63

EXPLANATION 

 • Explicit cursors are declared by and used by user to process multiple rows ,returned by select statement. • An explicit cursor is defined in the declaration section of the PL/SQL Block. It is created on a SELECT Statement which returns more than one row. We can provide a suitable name for the cursor.

Explicit Cursors

• 1)Declare the cursor • 2)Open the cursor • 3)Fetch data from cursor • 4)Close the cursor

 Explicit cursor management

• CURSOR cursor_name IS select_statement; • For e.g. • Cursor cursor_name is • Select name from emp where dept=‘maths’

Declaring the Cursor

• Open cursor_name • For e.g. • Open c_name • Where c_name is the name of cursor. • Open statement retrieves the records from db and places in the cursor(private sql area).

Opening the Cursor

• The fetch statement retrieves the rows from the active set one row at a time. The fetch statement is used usually used in conjunction with iterative process (looping statements) • Syntax: FETCH cursor-name INTO record-list • Ex: LOOP • ----------- • ------------ • FETCH c_name INTO name; • ----------- • END LOOP;

Fetching data from cursor

Closing a cursor: • Closing statement closes/deactivates/disables the previously opened cursor and makes the active set undefined. • Syntax : CLOSE cursor_name 69• 70. Cursor can store multiple rows at a time but without loop cursor

cannot fetch multiple rows but only print very first row from your result e.g. on next slide

Closing cursor

• A Package is a container that may have many functions and procedures within it. • A PACKAGE is a group of programmatic constructs combined. • A package is a schema object that groups logically related PL/SQL types, items, and subprograms

PACKAGES

THANK YOU

Contact IND : +91-9100998433/34 USA : +1(224) 676-9882Email ID : trainings@cheyat.com

Website : http://cheyat.com/Skype ID: cheyatTraining

top related