Beruflich Dokumente
Kultur Dokumente
On
PL/SQL
Submitted in Partial fulfillment for the award of
Bachelor of Engineering
Submitted to
RAJIV GANDHI PROUDYOGIKI VISHWAVIDYALAYA
BHOPAL (M.P)
Submitted by
Ashish raghuwanshi(0133cs071013)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL
SESSION-2010
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY, BHOPAL
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
This is to certify that the training report titled
“PL/SQL” submitted by “Ajeet shrivastv ” student of B.E.
“VII SEM” to the department of “ Computer science
&engineering” , SIRT during the period “1/7/2010” to
“2/8/2010” under the guidance of Sourabh Umalkar, He has
submitted this training report toward partial fulfillment of
the award to the degree of Bachelor of Engineering in “BE
IT” of Rajiv Gandhi Proudyogiki Vishwavidyalaya during
the academic year 20102011
Chapter 1
Introduction
Chapter 2
Technical details
About PL/SQL
PL/SQL ENVIRONMENT
BENEFIT OF PL/SQL
Higher Productivity
PL/SQL lets you write very compact code for manipulating data. In
the same way that scripting languages such as Perl can read,
transform, and write data from files, PL/SQL can query, transform,
and update data in a database. PL/SQL saves time on design and
debugging by offering a full range of software-engineering features,
such as exception handling, encapsulation, data hiding, and object-
oriented data types.
PL/SQL extends tools such as Oracle Forms. With PL/SQL in these
tools, you can use familiar language constructs to build
applications. For example, you can use an entire PL/SQL block in an
Oracle Forms trigger, instead of multiple trigger steps, macros, or
user exits. PL/SQL is the same in all environments. After you learn
PL/SQL with one Oracle tool, you can transfer your knowledge to
other tools.
ERROR
• The part of the block that declares variables, cursors, and sub-
blocks that are referenced in the execution and exception
sections. The declaration section is optional, but if you have
one, it must come before the execution and exception sections.
Execution section
Types of variable
PL/SQL variable:
- Scalar
- Composite
- Reference
- Lob (large object)
Scalar = scalar data types hold a single value. The main data types
are those are those that correspond to column types in oracle
server table; PL/SQL also supports Boolean variables.
Lob(large object) = lob data type hold value, called locator, that the
location of large object (such as graphic image ) that are stored
out of line . LOB data types are discussed in details later in this
course.
In a PL/SQL block, if you are querying a database table, you need to declare data
variables to hold column values returned from the query. The variables can be
declared of the same explicit data type as the column in the table.
Now we have a procedure Proc1, which queries the "dept" table to get the
department name for a given deptno. The code is as follows:
create or replace procedure proc1
as
v_deptname varchar2(20);
begin
select deptname into v_deptname
from dept
where deptno = 10;
exception
when no_data_found then
raise_application_error(-20001,'Dept 10 does not exist');
end;
This procedure will work fine until the table definition gets
altered, such that the column "deptname" reaches varchar2(30) . If the department
name for deptno =10 is longer than 20 characters in the table, then the procedure will
raise an unhandled exception of 'Value Error' because the variable v_deptname cannot
hold more than 20 characters. If you have lots of procedures
with v_deptnamedeclared as varchar2(20), you will need to modify all the
procedures.
To get around this painful process, you should
declare the variables as the column type rather than the explicit datatype of the
column. See the declaration below:
v_deptname dept.deptname%TYPE;
IDENTIFIER
OPERATORES in PL/SQL
EXAMPLE:
• Increment the counter for a loop.
v_count := v_count + 1;
• Set the value of a Boolean flag.
v_count := ( v_n1 = v_n2);
DECLARE
v_sal employee.sal%TYPE;
BEGIN
END;
Chapter 3
•INSERT
•UPDATE
•DELETE
•MERGE
Conditions of IF statements:
•IF- THEN -END IF
•IF -THEN -ELSE- END- IF
•IF- THEN-ELSE- END- IF
IF STATEMENTS
SYNTAX
IF condition1 THEN
statement1;
ELSE
IF condition2 THEN
statement2;
ELSE
IF condition3 THEN
statement3;
END IF;
END IF;
END IF;
Declaring a record:
(first_col_name
column_datatype,
second_col_name
column_datatype, ...);
CURSORS
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.
DECLARE
CURSOR emp_cur IS
SELECT *
FROM emp_tbl
CLOSE cursor_name;
When a cursor is opened, the first row becomes the current row.
When the data is fetched it is copied to the record or variables and
the logical pointer moves to the next row and it becomes the
current row. On every fetch statement, the pointer moves to the
next row. If you want to fetch after the last row, the program will
throw an error. When there is more than one row in a cursor we can
use loops along with explicit cursor attributes to fetch all the
records.
Exception Handling
1) Type of Exception
2) An Error Code
3) A message
Declaration section
BEGIN
Exception section
EXCEPTION
END;
RAISE_APPLICATION_ERROR ( )
RAISE_APPLICATION_ERROR is a built-in procedure in oracle which is
used to display the user-defined error messages along with the error
number whose range is in between -20000 and -20999.
Whenever a message is displayed using
RAISE_APPLICATION_ERROR, all previous transactions which are not
committed within the PL/SQL Block are rolled back automatically
(i.e. change due to INSERT, UPDATE, or DELETE statements).
RAISE_APPLICATION_ERROR raises an exception but does not handle
it.
RAISE_APPLICATION_ERROR is used for the following reasons,
a) to create a unique id for an user-defined exception.
b) to make the user-defined exception look like an Oracle error.
The General Syntax to use this procedure is:
RAISE_APPLICATION_ERROR (error_number, error_message);
Package
Modularity
Packages let you encapsulate logically related types, items, and
subprograms in a named PL/SQL module. Each package is easy to
understand, and the interfaces between packages are simple, clear,
and well defined. This aids application development.
Added Functionality
Packaged public variables and cursors persist for the duration of a
session. So, they can be shared by all subprograms that execute in
the environment. Also, they allow you to maintain data across
transactions without having to store it in the database.
Better Performance
When you call a packaged subprogram for the first time, the whole
package is loaded into memory. So, later calls to related
subprograms in the package require no disk I/O. Also, packages stop
cascading dependencies and thereby avoid unnecessary
recompiling. For example, if you change the implementation of a
packaged function, Oracle need not recompile the calling
subprograms because they do not depend on the package body.
Package
The package body implements the package spec. That is, the
package body contains the implementation of every cursor and
subprogram declared in the package spec. Keep in mind that
subprograms defined in a package body are accessible outside the
package only if their specs also appear in the package spec.
To match subprogram specs and bodies, PL/SQL does a token-by-
token comparison of their headers. So, except for white space, the
headers must match word for word. Otherwise, PL/SQL raises an
exception, as the following example shows:
CREATE PACKAGE emp_actions AS
...
PROCEDURE calc_bonus (date_hired emp.hiredate%TYPE, ...);
END emp_actions;
fID:=UTL_FILE.FOPEN(loc_in,file_in,'W');
UTL_FILE.FCLOSE (fid);
END;
UTL_FILE.IS_OPEN =
IF NOT UTL_FILE.IS_OPEN(v_filehandle)THENv_newline:='This is
the lineof text';
END IF;
Package Description
DBMS_ALERT
Notify a database event (asynchronous)
DBMS_APPLICATION_INFO
Register an application name with the database
For auditing or performance tracking.
V$SESSION/V$SESSION_LONGOPS
Application info can be pushed into
DBMS_AQ
Add a message (of a predefined object type) onto a queue
Or dequeue a message.
DBMS_AQADM
Administer a queue or queue table
For messages of a predefined object type.
DBMS_AQELM
Configure Advanced Queuing
Asynchronous notification by e-mail and HTTP. *
DBMS_BACKUP_RESTORE
Normalize filenames on Windows NT platforms.
DBMS_DDL
Access SQL DDL statements from a stored procedure,
Provides special administration operations
Not available as DDLs.
DBMS_DEBUG
Implement server-side debuggers and provide a way to
Debug server-side PL/SQL program units.
DBMS_DEFER
User interface to a replicated transactional deferred
RPC facility. Requires the Distributed Option.
DBMS_DEFER_QUERY
Permit querying the deferred remote procedure calls (RPC)
Queue data that is not exposed through views.
Requires the Distributed Option.
DMBS_DEFER_SYS
The system administrator interface to a replicated
Transactional deferred RPC facility.
Requires the Distributed Option.
DBMS_DESCRIBE
Describe the arguments of a stored procedure
With full name translation and security checking.
DBMS_DISTRIBUTED_TRUST_ADMIN
Maintain the Trusted Database List, which is used to
Determine if a privileged database link from a particular
Server can be accepted.
DBMS_ENCODE
Encode???
DBMS_FGA
Fine-grained security functions. *
DMBS_FLASHBACK
Flash back to a version of the database at a specified
Wall-clock time or a specified system change
Number (SCN). *
DBMS_HS_PASSTHROUGH
Send pass-through SQL statements to non-Oracle systems.
(Via Heterogeneous Services)
DBMS_IOT
Create a table into which references to the chained rows
For an Index Organized Table can be placed using the
ANALYZE command.
DBMS_JOB
Schedule PL/SQL procedures that you want performed at
Periodic intervals; also the job queue interface.
DBMS_LDAP
Functions and procedures to access data from
LDAP servers. *
DBMS_LIBCACHE
Prepares the library cache on an Oracle instance by
Extracting SQL and PL/SQL from a remote instance and
Compiling this SQL locally without execution. *
DBMS_LOB
General purpose routines for operations on Oracle Large
Object (LOBs) data types - BLOB, CLOB (read-write),
And BFILEs (read-only).
DBMS_LOCK
Request, convert and release locks through Oracle Lock
Management services.
DBMS_LOGMNR
Functions to initialize and run the log reader.
DBMS_LOGMNR_CDC_PUBLISH
Identify new data that has been added to, modified, or
Removed from, relational tables and publish the changed
Data in a form that is usable by an application. *
DBMS_LOGMNR_CDC_SUBSCRIBE
View and query the change data that was captured
And published with the DBMS_LOGMNR_CDC_PUBLISH package. *
DBMS_LOGMNR_D
Query the dictionary tables of the current database, and
Create a text based file containing their contents.
DBMS_METADATA
Retrieve complete database object definitions (metadata)
From the dictionary. *
DBMS_MVIEW
Refresh snapshots that are not part of the same
Refresh group and purge logs. DBMS_SNAPSHOT is a synonym.
DBMS_OBFUSCATION_TOOLKIT
Procedures for Data Encryption Standards.
DBMS_ODCI
Get the CPU cost of a user function based on the
Elapsed time of the function. *
DBMS_OFFLINE_OG
Public APIs for offline instantiation of master groups.
DBMS_OFFLINE_SNAPSHOT
Public APIs for offline instantiation of snapshots.
DBMS_OLAP
Procedures for summaries, dimensions, and query rewrites.
DBMS_ORACLE_TRACE_AGENT
Client callable interfaces to the Oracle TRACE
Instrumentation within the Oracle7 Server.
DBMS_ORACLE_TRACE_USER
Public access to the Oracle release 7 Server
Oracle TRACE instrumentation for the calling user.
DBMS_OUTLN
Interface for procedures and functions associated with
Management of stored outlines. Synonymous with OUTLN_PKG
DBMS_OUTLN_EDIT
Edit an invoker's rights package. *
DBMS_OUTPUT
Accumulate information in a buffer so that it can be
Retrieved out later.
DBMS_PCLXUTIL
Intra-partition parallelism for creating partition-wise
Local indexes.
DBMS_PIPE
A DBMS pipe service which enables messages to be sent
Between sessions.
DBMS_PROFILER
A Probe Profiler API to profile PL/SQL applications
And identify performance bottlenecks.
To install this run profload.sql(as SYS) and proftab.sql(as user)
DBMS_RANDOM
A built-in random number generator.
Options to generate random numbers within a range or distribution.
DBMS_RECTIFIER_DIFF
APIs used to detect and resolve data inconsistencies
Between two replicated sites.
DBMS_REDEFINITION
Reorganize a table (change it's structure) while it's
Still online and in use. *
DBMS_REFRESH
Create groups of snapshots that can be refreshed together
to a transitionally consistent point in time.
Requires the Distributed Option.
DBMS_REPAIR
Repair data corruption.
DBMS_REPCAT
Administer and update the replication catalog and environment.
Requires the Replication Option.
DBMS_REPCAT_ADMIN
Create users with the privileges needed by the symmetric
replication facility. Requires the Replication Option.
DBMS_REPCAT_INSTATIATE
Instantiates deployment templates.
Requires the Replication Option.
DBMS_REPCAT_RGT
Define and maintain refresh group templates.
Requires the Replication Option.
DBMS_REPUTIL
Generate shadow tables, triggers, and packages
For table replication.
DBMS_RESOURCE_MANAGER
Maintain plans, consumer groups, and plan directives;
Also provides semantics so that you may group together
Changes to the plan schema.
DBMS_RESOURCE_MANAGER_PRIVS
Maintain privileges associated with resource consumer groups.
DBMS_RESUMABLE
Suspend large operations that run out of space or reach space
Limits after executing for a long time, fix the problem, and
Make the statement resume execution.
DBMS_RLS
Row level security administrative interface.
CHAPTER 4
TECHNOLOGY DETAILS
What few developers realized at the time was that the original
motivation and driving vision behind PL/SQL extended beyond the
desire to offer programmatic control within products like
SQL*Forms. Very early in the life cycle of Oracle's database and
tools, Oracle Corporation had recognized two key weaknesses in
their architecture: lack of portability and problems with execution
authority.
4.1.1 Improved Application Portability with PL/SQL
The concern about portability might seem odd to those of us
familiar with Oracle Corporation's marketing and technical
strategies. One of the hallmarks of the Oracle solution from the
early 1980s was its portability. At the time that PL/SQL came along,
the C-based RDBMS ran on many different operating systems and
hardware platforms.
The PL/SQL language was (and is) intended to widen the range of
application requirements which can be handled entirely in
operating-system independent programming tools.
Version 1.0
First available in SQL*Plus as a batch-processing script.
Oracle Version 6.0 was released at approximately the same
time. PL/SQL was then implemented
within SQL*Forms Version 3, the
predecessor of Oracle Forms.
Release 1.1
Available only in the Oracle Developer/2000 Release 1 tool
this upgrade supports client-side packages and allows
client-side programs to execute stored code transparently.
Version 2.0
Available with Release 7.0 (Oracle Server).Major upgrade to
Version 1. Adds support for stored procedures, functions,
packages, programmer-defined records, PL/SQL tables, and
many package extensions, including DBMS_OUTPUT and
DBMS_PIPE.
Release 2.1
Available with Release 7.1 of the Oracle Server Version.
Supports programmer-defined subtypes, enables the use of
stored functions inside SQL statements, and offers dynamic
SQL with the DBMS_SQL package. With Version 2.1, you can
now execute SQL DDL statements from within PL/SQL
programs.
Release 2.2
Available with Release 7.2 of the Oracle Server Version.
Implements a binary "wrapper" for PL/SQL programs to
protect source code, supports cursor variables for
embedded PL/SQL environments such as Pro*C, and makes
available database-driven job scheduling with the
DBMS_JOB package.
Release 2.3
Available with Release 7.3 of the Oracle Server Version.
Enhances functionality of PL/SQL tables, offers improved
remote dependency management, adds file I/O capabilities
to PL/SQL, and completes the implementation of cursor
variables.
Chapter 6
Conclusion & Result:
After completion of this practical training I conclude that
training in the Engineering is an essential task for each and
every student and it must be taken seriously.
The practical training is a chance to the trainee to gain the
practical knowledge in the field and to work in the
scheduled environment. Apart from these things it
improves the managing qualities, which are essential for an
engineer.
It is very fruitful experience for me to overcome my
weaknesses and how to face and solve the difficulties
arises in the field.
Table of Contents
Topics Page No .
Acknowledgments………………………………….…1
Description……………………………………………..4
Chapter 1: Introduction………..……………….….5