0% found this document useful (0 votes)
11 views

pl sql

The document provides an overview of PL/SQL, including its basic syntax, data types, variable declarations, and control structures such as loops and conditional statements. It explains the structure of PL/SQL blocks, the use of scalar and large object data types, and the concept of user-defined subtypes. Additionally, it covers the handling of NULL values, variable scope, and the use of cursors in PL/SQL programming.

Uploaded by

vpabdulazeez8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

pl sql

The document provides an overview of PL/SQL, including its basic syntax, data types, variable declarations, and control structures such as loops and conditional statements. It explains the structure of PL/SQL blocks, the use of scalar and large object data types, and the concept of user-defined subtypes. Additionally, it covers the handling of NULL values, variable scope, and the use of cursors in PL/SQL programming.

Uploaded by

vpabdulazeez8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Program with SQL

PL/SQL - Basic Syntax


PL/SQL is a block-structured language, meaning that PL/SQL programs are divided and written in
logical blocks of code. Each block consists of three sub-parts:
Declarations
This section starts with the keyword DECLARE. It is an optional section and defines all variables,
cursors, subprograms, and other elements to be used in the program.
ExecutableCommands
This section is enclosed between the keywords BEGIN and END and it is a mandatory section. It
consists of the executable PL/SQL statements of the program. It should have at least one executable line of
code, which may be just a NULL command to indicate that nothing should be executed.
ExceptionHandling
This section starts with the keyword EXCEPTION. This section is again optional and contains exception(s)
that handle errors in the program.

Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL
blocks using BEGIN and END. Here is the basic structure of a PL/SQL block:
DECLARE
The 'Hello World' Example:
<declarations section>
BEGIN DECLARE
<executable command(s)> message varchar2(20):= 'Hello, World!';
EXCEPTION BEGIN
<exception handling> dbms_output.put_line(message);
END; END;
/
The end; line signals the end of the PL/SQL block. To run the code from SQL command line, you may need to
type / at the beginning of the first blank line after the last line of the code. When the above code is executed at
SQL prompt, it produces following result: Hello World
PL/SQL - Data Types
PL/SQL variables, constants and parameters must have a valid data type which specifies a storage
format, constraints, and valid range of values. This tutorial will take you through SCALAR and LOB data types
available in PL/SQL and other two data types will be covered in other chapters.
Category Description
Single values with no internal components, such as a NUMBER, DATE, or
Scalar
BOOLEAN.
Pointers to large objects that are stored separately from other data items, such as
Large Object (LOB)
text, graphic images, video clips, and sound waveforms.
Data items that have internal components that can be accessed individually. For
Composite
example, collections and records.
Reference Pointers to other data items.
PL/SQL Scalar Data Types and Subtypes
PL/SQL Scalar Data Types and Subtypes come under the following categories:
Date Type Description
Numeric Numeric values on which arithmetic operations are performed.
Character Alphanumeric values that represent single characters or strings of characters.
Boolean Logical values on which logical operations are performed.
Datetime Dates and times.
PL/SQL provides subtypes of data types. For example, the data type NUMBER has a subtype called INTEGER.
Following is a valid declaration:
DECLARE
num1 INTEGER;
num2 REAL;
num3 DOUBLE PRECISION;
BEGIN
null;
END;
/
Built-in SQL functions (such as TO_CHAR)
PL/SQL functions invoked from SQL statements
PL/SQL Datetime and Interval Types
The DATE datatype to store fixed-length datetimes, which include the time of day in seconds since
midnight. Valid dates range from January 1, 4712 BC to December 31, 9999 AD.
The default date format is set by the Oracle initialization parameter NLS_DATE_FORMAT. For example, the
default might be 'DD-MON-YY', which includes a two-digit number for the day of the month, an abbreviation of
the month name, and the last two digits of the year, for example, 01-OCT-12.
Each DATE includes the century, year, month, day, hour, minute, and second.
PL/SQL Large Object (LOB) Data Types
Large object (LOB) data types refer large to data items such as text, graphic images, video clips, and
sound waveforms. LOB data types allow efficient, random, piecewise access to this data. Following are the
predefined PL/SQL LOB data types:
Data Type Description Size
Used to store large binary objects in operating system System-dependent. Cannot
BFILE
files outside the database. exceed 4 gigabytes (GB).
BLOB Used to store large binary objects in the database. 8 to 128 terabytes (TB)
Used to store large blocks of character data in the
CLOB 8 to 128 TB
database.
Used to store large blocks of NCHAR data in the
NCLOB 8 to 128 TB
database.
PL/SQL User-Defined Subtypes
A subtype is a subset of another data type, which is called its base type. A subtype has the same valid
operations as its base type, but only a subset of its valid values.
PL/SQL predefines several subtypes in package STANDARD. For example, PL/SQL predefines the subtypes
CHARACTER and INTEGER as follows:
SUBTYPE CHARACTER IS CHAR;
SUBTYPE INTEGER IS NUMBER(38,0);
You can define and use your own subtypes. The following program illustrates defining and using a user-
defined subtype:
DECLARE
SUBTYPE name IS char(20);
SUBTYPE message IS varchar2(100);
salutation name;
greetings message;
BEGIN
salutation := 'Reader ';
greetings := 'Welcome to the World of PL/SQL';
dbms_output.put_line('Hello ' || salutation || greetings);
END;
/
When the above code is executed at SQL prompt, it produces the following result:
Hello Reader Welcome to the World of PL/SQL

NULLs in PL/SQL
PL/SQL NULL values represent missing or unknown data and they are not an integer, a character, or
any other specific data type. Note that NULL is not the same as an empty data string or the null character value
'\0'. A null can be assigned but it cannot be equated with anything, including itself.
PL/SQL - Variables
The name of a PL/SQL variable consists of a letter optionally followed by more letters, numerals, dollar
signs, underscores, and number signs and should not exceed 30 characters. By default, variable names are not
case-sensitive. You cannot use a reserved PL/SQL keyword as a variable name.
Variable Declaration in PL/SQL
PL/SQL variables must be declared in the declaration section or in a package as a global variable. When you
declare a variable, PL/SQL allocates memory for the variable's value and the storage location is identified by the
variable name.
The syntax for declaring a variable is:
variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]
Where, variable_name is a valid identifier in PL/SQL, datatype must be a valid PL/SQL data type or any user
defined data type which we already have discussed in last chapter. Some valid variable declarations along with
their definition are shown below:
sales number(10, 2);
pi CONSTANT double precision := 3.1415;
name varchar2(25);
address varchar2(100);

Variable Scope in PL/SQL


PL/SQL allows the nesting of Blocks, i.e., each program block may contain another inner block. If a variable is
declared within an inner block, it is not accessible to the outer block. However, if a variable is declared and
accessible to an outer Block, it is also accessible to all nested inner Blocks. There are two types of variable scope:
Local variables - variables declared in an inner block and not accessible to outer blocks.
Global variables - variables declared in the outermost block or a package.
Following example shows the usage of Local and Global variables in its simple form:
DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 195;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;
/
When the above code is executed, it produces the following result:
Outer Variable num1: 95
Outer Variable num2: 85
Inner Variable num1: 195
Inner Variable num2: 185

PL/SQL - Conditions
Decision-making structures require that the programmer specify one or more conditions to be evaluated
or tested by the program, along with a statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is determined to be false.
IF-THEN Statement
It is the simplest form of IF control statement, frequently used in decision making and changing the
control flow of the program execution.
The IF statement associates a condition with a sequence of statements enclosed by the keywords THEN and END
IF. If the condition is TRUE, the statements get executed, and if the condition is FALSE or NULL, then the IF
statement does nothing.
Syntax for IF-THEN statement is:
IF condition THEN
S;
END IF;
Where condition is a Boolean or relational condition and S is a simple or compound statement. Example
of an IF-THEN statement is:
IF (a <= 20) THEN
c:= c+1;
END IF;
If the Boolean expression condition evaluates to true, then the block of code inside the if statement will be
executed. If Boolean expression evaluates to false, then the first set of code after the end of the if statement (after
the closing end if) will be executed.
IF-THEN-ELSE Statement
A sequence of IF-THEN statements can be followed by an optional sequence of ELSE statements, which
execute when the condition is FALSE.
Syntax for the IF-THEN-ELSE statement is:

IF condition THEN
S1;
ELSE
S2;
END IF;
Where, S1 and S2 are different sequence of statements. In the IF-THEN-ELSE statements, when the test
condition is TRUE, the statement S1 is executed and S2 is skipped; when the test condition is FALSE, then S1 is
bypassed and statement S2 is executed. For example,
IF color = red THEN
dbms_output.put_line('You have chosen a red car')
ELSE
dbms_output.put_line('Please choose a color for your car');
END IF;
If the Boolean expression condition evaluates to true, then the if-then block of code will be executed, otherwise
the else block of code will be executed.
IF-THEN-ELSIF Statement
The IF-THEN-ELSIF statement allows you to choose between several alternatives. An IF-THEN
statement can be followed by an optional ELSIF...ELSE statement. The ELSIF clause lets you add additional
conditions.
When using IF-THEN-ELSIF statements there are few points to keep in mind.
It's ELSIF, not ELSEIF
An IF-THEN statement can have zero or one ELSE's and it must come after any ELSIF's.
An IF-THEN statement can have zero to many ELSIF's and they must come before the ELSE.
Once an ELSIF succeeds, none of the remaining ELSIF's or ELSE's will be tested.
The syntax of an IF-THEN-ELSIF Statement in PL/SQL programming language is:
IF(boolean_expression 1)THEN
S1; -- Executes when the boolean expression 1 is true
ELSIF(boolean_expression 2) THEN
S2; -- Executes when the boolean expression 2 is true
ELSIF(boolean_expression 3) THEN
S3; -- Executes when the boolean expression 3 is true
ELSE
S4; -- executes when the none of the above condition is true
END IF;

Nested IF-THEN-ELSE Statements


It is always legal in PL/SQL programming to nest IF-ELSE statements, which means you can use one IF
or ELSE IF statement inside another IF or ELSE IF statement(s).
Syntax:
IF(boolean_expression 1)THEN
-- executes when the boolean expression 1 is true
IF(boolean_expression 2) THEN
-- executes when the boolean expression 2 is true
sequence-of-statements;
END IF;
ELSE
-- executes when the boolean expression 1 is not true
else-statements;
END IF;
PL/SQL - Loops
There may be a situation when you need to execute a block of code several number of times. In general,
statements are executed sequentially: The first statement in a function is executed first, followed by the second,
and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the
general form of a loop statement in most of the programming languages:
WHILE LOOP Statement
A WHILE LOOP statement in PL/SQL programming language repeatedly executes a target statement as
long as a given condition is true.
Syntax:
WHILE condition LOOP
sequence_of_statements
END LOOP;
FOR LOOP Statement
A FOR LOOP is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
Syntax:
FOR counter IN initial_value ..final_value LOOP
sequence_of_statements;
END LOOP;
Following are some special characteristics of PL/SQL for loop:
The initial_value and final_value of the loop variable or counter can be literals, variables, or expressions but must
evaluate to numbers. Otherwise, PL/SQL raises the predefined exception VALUE_ERROR.
The initial_value need not to be 1; however, the loop counter increment (or decrement) must be 1.
PL/SQL allows determine the loop range dynamically at run time.
Nested Loops
PL/SQL allows using one loop inside another loop. Following section shows few examples to illustrate the
concept.
The syntax for a nested FOR LOOP statement in PL/SQL is as follows:
FOR counter1 IN initial_value1 ..final_value1 LOOP
sequence_of_statements1
FOR counter2 IN initial_value2 ..final_value2 LOOP
sequence_of_statements2
END LOOP;
END LOOP;
The syntax for a nested WHILE LOOP statement in Pascal is as follows:
WHILE condition1 LOOP
sequence_of_statements1
WHILE condition2 LOOP
sequence_of_statements2
END LOOP;
END LOOP;
EXIT Statement
The EXIT statement in PL/SQL programming language has following two usages:
When the EXIT statement is encountered inside a loop, the loop is immediately terminated and program control
resumes at the next statement following the loop.
If you are using nested loops (i.e. one loop inside another loop), the EXIT statement will stop the execution of the
innermost loop and start executing the next line of code after the block.
Syntax:
The syntax for a EXIT statement in PL/SQL is as follows:
EXIT;
GOTO Statement
A GOTO statement in PL/SQL programming language provides an unconditional jump from the GOTO
to a labeled statement in the same subprogram.
NOTE: Use of GOTO statement is highly discouraged in any programming language because it makes difficult
to trace the control flow of a program, making the program hard to understand and hard to modify. Any
program that uses a GOTO can be rewritten so that it doesn't need the GOTO.
The syntax for a GOTO statement in PL/SQL is as follows:
GOTO label;
..
..
<<label>>
statement;
Cursor
Oracle creates a memory area, known as context area, for processing an SQL statement, which contains
all information needed for processing the statement, for example, number of rows processed, etc. A cursor is a
pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds the rows (one or
more) returned by a SQL statement. The set of rows the cursor holds is referred to as the active set.You can
name a cursor so that it could be referred to in a program to fetch and process the rows returned by the SQL
statement, one at a time. There are two types of cursors:
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there
is no explicit cursor for the statement. Programmers cannot control the implicit cursors and the information in
it.
Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor is associated with
this statement. For INSERT operations, the cursor holds the data that needs to be inserted. For UPDATE and
DELETE operations, the cursor identifies the rows that would be affected.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which always has the attributes
like %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT. The SQL cursor has additional attributes,
%BULK_ROWCOUNT and %BULK_EXCEPTIONS, designed for use with the FORALL statement. The
following table provides the description of the most used attributes:
Attribute Description
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or
%FOUND more rows or a SELECT INTO statement returned one or more rows. Otherwise, it
returns FALSE.
The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or
%NOTFOUND DELETE statement affected no rows, or a SELECT INTO statement returned no
rows. Otherwise, it returns FALSE.
Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
%ISOPEN
automatically after executing its associated SQL statement.
Returns the number of rows affected by an INSERT, UPDATE, or DELETE
%ROWCOUNT
statement, or returned by a SELECT INTO statement.
Explicit Cursors
Explicit cursors are programmer defined cursors for gaining more control over the context area. An
explicit cursor should be defined in the declaration section of the PL/SQL Block. It is created on a SELECT
Statement which returns more than one row.
The syntax for creating an explicit cursor is :
CURSOR cursor_name IS select_statement;
Working with an explicit cursor involves four steps:
Declaring the cursor for initializing in the memory
Opening the cursor for allocating memory
Fetching the cursor for retrieving data
Closing the cursor to release allocated memory
Declaring the Cursor
Declaring the cursor defines the cursor with a name and the associated SELECT statement. For example:
CURSOR c_customers IS SELECT id, name, address FROM customers;
Opening the Cursor
Opening the cursor allocates memory for the cursor and makes it ready for fetching the rows returned by
the SQL statement into it. For example, we will open above-defined cursor as follows:
OPEN c_customers;
Fetching the Cursor
Fetching the cursor involves accessing one row at a time. For example we will fetch rows from the above-
opened cursor as follows:
FETCH c_customers INTO c_id,c_name,c_addr;
Closing the Cursor
Closing the cursor means releasing the allocated memory. For example, we will close above-opened
cursor as follows: CLOSE c_customers;
All access to cursors goes through cursor variables, which are always of the special data type refcursor
One way to create a cursor variable is just to declare it as a variable of type refcursor.
curs1 refcursor;
this can be used with any query, therefore it is known as unbound cursor.
Another way is to use the cursor declaration syntax, which in general is:
CURSOR <cursor_name> IS <SELECT stmt>
This cursor is bound to the particular query so it is known as bound cursor.
Cursor Video Link: https://www.youtube.com/watch?v=ZPAe3c69rIw

Triggers
Triggers are stored programs, which are automatically executed or fired when some events occur.
Triggers are, in fact, written to be executed in response to any of the following events:
A database manipulation (DML) statement (DELETE, INSERT, or UPDATE).
A database definition (DDL) statement (CREATE, ALTER, or DROP).
A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
Benefits of Triggers
Triggers can be written for the following purposes:
Generating some derived column values automatically
Enforcing referential integrity
Event logging and storing information on table access
Auditing
Synchronous replication of tables
Imposing security authorizations
Preventing invalid transactions
There are two types of triggers based on the which level it is triggered.
1) Row level trigger - An event is triggered for each row upated, inserted or deleted.
2) Statement level trigger - An event is triggered for each sql statement executed.

PL/SQL Trigger Execution Hierarchy


The following hierarchy is followed when a trigger is fired.
1) BEFORE statement trigger fires first.
2) Next BEFORE row level trigger fires, once for each row affected.
3) Then AFTER row level trigger fires once for each affected row. This events will alternates between
BEFORE and AFTER row level triggers.
4) Finally the AFTER statement level trigger fires.
For Example: Let's create a table 'product_check' which we can use to store messages when triggers are fired.
CREATE TABLE product(Message varchar2(50), Current_Datenumber(32));

Let's create a BEFORE and AFTER statement and row level triggers for the product table.
1) BEFORE UPDATE, Statement Level: This trigger will insert a record into the table 'product_check' before a
sql update statement is executed, at the statement level.
CREATE or REPLACE TRIGGER Before_Update_Stat_product
BEFORE
UPDATE ON product
Begin
INSERT INTO product_check
Values('Before update, statement level',sysdate);
END;
/
2) BEFORE UPDATE, Row Level: This trigger will insert a record into the table 'product_check' before each
row is updated.
CREATE or REPLACE TRIGGER Before_Upddate_Row_product
BEFORE
UPDATE ON product
FOR EACH ROW
BEGIN
INSERT INTO product_check
Values('Before update row level',sysdate);
END;
/
3) AFTER UPDATE, Statement Level: This trigger will insert a record into the table 'product_check' after a sql
update statement is executed, at the statement level.
CREATE or REPLACE TRIGGER After_Update_Stat_product
AFTER
UPDATE ON product
BEGIN
INSERT INTO product_check
Values('After update, statement level', sysdate);
End;
/
4) AFTER UPDATE, Row Level: This trigger will insert a record into the table 'product_check' after each row is
updated.
CREATE or REPLACE TRIGGER After_Update_Row_product
AFTER
insert On product
FOR EACH ROW
BEGIN
INSERT INTO product_check
Values('After update, Row level',sysdate);
END;
/
Now lets execute a update statement on table product.
UPDATE PRODUCT SET unit_price = 800
WHERE product_id in (100,101);
Lets check the data in 'product_check' table to see the order in which the trigger is fired.

SELECT * FROM product_check;

Output:
Mesage Current_Date
------------------------------------------------------------
Before update, statement level 26-Nov-2008
Before update, row level 26-Nov-2008
After update, Row level 26-Nov-2008
Before update, row level 26-Nov-2008
After update, Row level 26-Nov-2008
After update, statement level 26-Nov-2008
The above result shows 'before update' and 'after update' row level events have occured twice, since two records
were updated. But 'before update' and 'after update' statement level events are fired only once per sql statement.
The above rules apply similarly for INSERT and DELETE statements.
How To know Information about Triggers.
We can use the data dictionary view 'USER_TRIGGERS' to obtain information about any trigger.
The below statement shows the structure of the view 'USER_TRIGGERS'
DESC USER_TRIGGERS;
NAME Type
--------------------------------------------------------
TRIGGER_NAME VARCHAR2(30)
TRIGGER_TYPE VARCHAR2(16)
TRIGGER_EVENT VARCHAR2(75)
TABLE_OWNER VARCHAR2(30)
BASE_OBJECT_TYPE VARCHAR2(16)
TABLE_NAME VARCHAR2(30)
COLUMN_NAME VARCHAR2(4000)
REFERENCING_NAMES VARCHAR2(128)
WHEN_CLAUSE VARCHAR2(4000)
STATUS VARCHAR2(8)
DESCRIPTION VARCHAR2(4000)
ACTION_TYPE VARCHAR2(11)
TRIGGER_BODY LONG
This view stores information about header and body of the trigger.
SELECT * FROM user_triggers WHERE trigger_name = 'Before_Update_Stat_product';
The above sql query provides the header and body of the trigger 'Before_Update_Stat_product'.
You can drop a trigger using the following command.
DROP TRIGGER trigger_name;
CYCLIC CASCADING in a TRIGGER
This is an undesirable situation where more than one trigger enter into an infinite loop. while creating a
trigger we should ensure the such a situtation does not exist.
The below example shows how Trigger's can enter into cyclic cascading.
Let's consider we have two tables 'abc' and 'xyz'. Two triggers are created.
1) The INSERT Trigger, triggerA on table 'abc' issues an UPDATE on table 'xyz'.
2) The UPDATE Trigger, triggerB on table 'xyz' issues an INSERT on table 'abc'.
In such a situation, when there is a row inserted in table 'abc', triggerA fires and will update table 'xyz'.
When the table 'xyz' is updated, triggerB fires and will insert a row in table 'abc'.
This cyclic situation continues and will enter into a infinite loop, which will crash the databa
Stored Procedure
Overview
A stored procedure is nothing more than prepared SQL code that you save so you can reuse the
code over and over again. So if you think about a query that you write over and over again, instead of having to
write that query each time you would save it as a stored procedure and then just call the stored procedure to
execute the SQL code that you saved as part of the stored procedure.
In addition to running the same SQL code over and over again you also have the ability to pass parameters to the
stored procedure, so depending on what the need is the stored procedure can act accordingly based on the
parameter values that were passed.
A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific
task. This is similar to a procedure in other programming languages.
A procedure has a header and a body. The header consists of the name of the procedure and the parameters or
variables passed to the procedure. The body consists or declaration section, execution section and exception
section similar to a general PL/SQL Block.
A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage.
Parameters in Procedure and Functions
In PL/SQL, we can pass parameters to procedures and functions in three ways.
1) IN type parameter: These types of parameters are used to send values to stored procedures.
2) OUT type parameter: These types of parameters are used to get values from stored procedures. This is similar
to a return type in functions.
3) IN OUT parameter: These types of parameters are used to send values and get values from stored procedures.
NOTE: If a parameter is not explicitly defined a parameter type, then by default it is an IN type parameter.
1) IN parameter:
This is similar to passing parameters in programming languages. We can pass values to the stored procedure
through these parameters or variables. This type of parameter is a read only parameter. We can assign the value
of IN type parameter to a variable or use it in a query, but we cannot change its value inside the procedure.
General syntax to pass a IN parameter is

CREATE [OR REPLACE] PROCEDURE procedure_name ( param_name1 IN datatype, param_name12 IN


datatype ... )param_name1, •param_name2... are unique parameter names.
datatype - defines the datatype of the variable.
IN - is optional, by default it is a IN type parameter.

2) OUT Parameter:
The OUT parameters are used to send the OUTPUT from a procedure or a function. This is a write-only
parameter i.e, we cannot pass values to OUT paramters while executing the stored procedure, but we can assign
values to OUT parameter inside the stored procedure and the calling program can recieve this output value.
The General syntax to create an OUT parameter is
CREATE [OR REPLACE] PROCEDURE proc2 (param_name OUT datatype)
The parameter should be explicity declared as OUT parameter.

3) IN OUT Parameter:
The IN OUT parameter allows us to pass values into a procedure and get output values from the procedure. This
parameter is used if the value of the IN parameter can be changed in the calling program.
By using IN OUT parameter we can pass values into a parameter and return a value to the calling program
using the same parameter. But this is possible only if the value passed to the procedure and output value have a
same datatype. This parameter is used if the value of the parameter will be changed in the procedure.
The General syntax to create an IN OUT parameter is
CREATE [OR REPLACE] PROCEDURE proc3 (param_name IN OUT datatype)
There are two ways to execute a procedure.
1) From the SQL prompt.
EXECUTE [or EXEC] procedure_name;
2) Within another procedure – simply use the procedure name.
procedure_name;

Exception Handling
Exceptions are runtime errors or unexpected events that occur during the execution of a
PL/SQL code block. PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL
Block known as exception Handling.The oracle engine is the first one to identify such an exception
and it immediately tries to resolve it by default exception handler.The default exception handler is a
block of code predefined in the memory to take the appropriate action against exceptions.
Exception handling can be done in the EXCEPTION part of PL/SQL program code block.
Following is the syntax for it:
DECLARE -- Declaration statements; BEGIN -- SQL statements; -- Procedural statements;
EXCEPTION -- Exception handling statements; END;

PL/SQL Exception message consists of three parts.


1) Type of Exception
2) An Error Code
3) A message

Structure of Exception Handling.


General Syntax for coding the exception section
DECLARE
Declaration section
BEGIN
Exception section
EXCEPTION
WHEN ex_name1 THEN
-Error handling statements
WHEN ex_name2 THEN
-Error handling statements
WHEN Others THEN
-Error handling statements
END;
General PL/SQL statments can be used in the Exception Block.
When an exception is raised, Oracle searches for an appropriate exception handler in the exception section. For
example in the above example, if the error raised is 'ex_name1 ', then the error is handled according to the
statements under it. Since, it is not possible to determine all the possible runtime errors during testing fo the
code, the 'WHEN Others' exception is used to manage the exceptions that are not explicitly handled. Only one
exception can be raised in a Block and the control does not return to the Execution Section after the error is
handled.
Types of Exception.
There are 3 types of Exceptions.
a) Named System Exceptions
b) Unnamed System Exceptions
c) User-defined Exceptions
a) Named System Exceptions
System exceptions are automatically raised by Oracle, when a program violates a RDBMS rule. There
are some system exceptions which are raised frequently, so they are pre-defined and given a name in Oracle
which are known as Named System Exceptions.
For example: NO_DATA_FOUND and ZERO_DIVIDE are called Named System exceptions.
Named system exceptions are:
1) Not Declared explicitly,
2) Raised implicitly when a predefined Oracle error occurs,
3) caught by referencing the standard name within an exception-handling routine.
b) Unnamed System Exceptions
Those system exception for which oracle does not provide a name is known as unamed system exception.
These exception do not occur frequently. These Exceptions have a code and an associated message.
There are two ways to handle unnamed sysyem exceptions:
1. By using the WHEN OTHERS exception handler, or
2. By associating the exception code to a name and using it as a named exception.
c) User-defined Exceptions
Apart from sytem exceptions we can explicity define exceptions based on business rules. These are known as
user-defined exceptions.
Steps to be followed to use user-defined exceptions:
• They should be explicitly declared in the declaration section.
• They should be explicitly raised in the Execution Section.
• They should be handled by referencing the user-defined exception name in the exception section.
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).

Locks :
Locks are mechanism used to ensure data integrity. The oracle engine automatically locks table
data while executing SQL statements like Select/insert/UPDATE/DELETE. This type of locking is
called implicit locking
There are two types of Locks
1)Shared lock
2)Exclusive lock

Shared lock:
Shared locks are placed on resources whenever a read operation (select) is performed.
Multiple shared locks can be simultaneously set on a resource.
Exclusive lock:
Exclusive locks are placed on resources whenever a write operation (INSERT, UPDATE And DELETE) are performed.
Only one exclusive lock can be placed on a resource at a time.
i.e. the first user who acquires an exclusive lock will continue to have the sole ownership of the resource, and no
other user can acquire an exclusive lock on that resource
Levels of Locks:
Oracle does not provide a field level lock.
Oracle provides the following three levels of Locking.
a)Row level
b)Page level
c)Table level
Row Level locking
If the WHERE clause evaluates to only one row in the table, a row level lock is used.
Page Level locking
If the WHERE clause evaluates to a set of data, a page level lock is used.
Table Level locking
If there is no WHERE clause, the query accesses the entire table, a table level lock is used.
Can't update entire table data when update is done by other user.
Syntax:
LOCK TABLE <tablename> [<tablename>]….. IN { ROW SHARE / ROW EXCLUSIVE / SHARE UPDATE / SHARE / SHARE
ROW EXCLUSIVE / EXCLUSIVE}[NOWAIT]
Exclusive lock:
They allow query on the locked resource but prohibit any other activity
Example:
Run SQL Command Line
SQL> Lock table client_master IN Exclusive Mode NOWAIT;
Table(s) Locked.
Deadlock:
In a deadlock, two database operations wait for each other to release a lock.
A deadlock occurs when two users have a lock, each on a separate object, and, they want to acquire a lock on each
other's object.
When this happens, the first user has to wait for the second user to release the lock, but the second user will not
release it until the lock on the first user's object is freed. At this point, both the users are at an impasse and cannot
proceed with their business.
In such a case, Oracle detects the deadlock automatically and solves the problem by aborting one of the two
transactions.
example :
Transaction 1 BEGIN UPDATE client_master SET salary = 500 WHERE client_no=’c1’; UPDATE client_master SET
salary = 500 WHERE client_no=’c2’; END Transaction 2 BEGIN UPDATE client_master SET salary = 5000 WHERE
client_no=’c2’; UPDATE client_master SET salary = 500 WHERE client_no=’c1’; END
Assume that Transaction 1 and Transaction2 begin exactly at the same time.
By default Oracle automatically places exclusive lock on data that is being updated.
This causes Transaction 1 to wait for Transaction2 to complete but in turn Transaction2 has to wait for Transaction 1
to complete.

For more Study materials ,Please visit


Our Website: www.bcamcas.in

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy