pl sql-ch2

12
[email protected] Prof. Mukesh N. Tekwani 1 Chap 2. PL/SQL Transactions and Cursors 2. PL/SQL Transactions And Cursors 1. What is a transaction? A transaction is a series of one or more SQL statements that are logically related or a series of operations performed on an Oracle table. This logical unit is treated as an entity by Oracle. All changes to the data table are viewed as a two-step process. Step 1: Make the changes requested, and Step 2: This must be followed by the COMMIT statement. A ROLLBACK statement given at the SQL prompt can be used to undo a part of or the entire transaction. A transaction begins with the first executable SQL statement after a COMMIT, ROLLBACK, or connection made to the Oracle engine. A transaction is a group of events that occur between any of the following events: 1. Connecting to Oracle 2. Disconnecting from Oracle 3. Committing changes to the database table 4. Rollback Closing transaction: a transaction is closed by either a commit or a rollback statement. COMMIT statement: This statement ends the current transaction and changes are made permanent. Syntax: COMMIT ROLLBACK statement: This statement does exactly the opposite of the COMMIT statement. It ends the transaction but undoes any changes made during the transaction. All transactional locks are released from the tables. Syntax: ROLLBACK [TO [SAVEPOINT] <savepointname>]; SAVEPOINT is optional and is used to roll back a transaction partially. savepointname is a savepoint ceated during the current transaction. 2. Write a note on ROLLBACK and SAVEPOINT clauses. ROLLBACK statement: This statement does exactly the opposite of the COMMIT statement. It ends the transaction but undoes any changes made during the transaction. All transactional locks are released from the tables. Syntax: ROLLBACK [TO [SAVEPOINT] <savepointname>]; SAVEPOINT is optional and is used to roll back a transaction partially. savepointname is a savepoint ceated during the current transaction. Creating a SAVEPOINT: SAVEPOINT is used to mark and save the current point in the processing of a transaction. If a SAVEPOINT is used with a ROLLBACK statement, parts of a transaction can be undone. An

Upload: mukesh-tekwani

Post on 12-Nov-2014

2.611 views

Category:

Education


2 download

DESCRIPTION

PL/SQL Chapter 2

TRANSCRIPT

Page 1: Pl sql-ch2

[email protected] Prof. Mukesh N. Tekwani

1 Chap 2. PL/SQL Transactions and Cursors

2. PL/SQL Transactions And Cursors

1. What is a transaction?

A transaction is a series of one or more SQL statements that are logically related or a series of

operations performed on an Oracle table.

This logical unit is treated as an entity by Oracle. All changes to the data table are viewed as a

two-step process. Step 1: Make the changes requested, and Step 2: This must be followed by

the COMMIT statement. A ROLLBACK statement given at the SQL prompt can be used to

undo a part of or the entire transaction.

A transaction begins with the first executable SQL statement after a COMMIT, ROLLBACK,

or connection made to the Oracle engine.

A transaction is a group of events that occur between any of the following events:

1. Connecting to Oracle

2. Disconnecting from Oracle

3. Committing changes to the database table

4. Rollback

Closing transaction: a transaction is closed by either a commit or a rollback statement.

COMMIT statement: This statement ends the current transaction and changes are made

permanent.

Syntax:

COMMIT

ROLLBACK statement: This statement does exactly the opposite of the COMMIT statement.

It ends the transaction but undoes any changes made during the transaction. All transactional

locks are released from the tables.

Syntax:

ROLLBACK [TO [SAVEPOINT] <savepointname>];

SAVEPOINT is optional and is used to roll back a transaction partially.

savepointname is a savepoint ceated during the current transaction.

2. Write a note on ROLLBACK and SAVEPOINT clauses.

ROLLBACK statement: This statement does exactly the opposite of the COMMIT statement.

It ends the transaction but undoes any changes made during the transaction. All transactional

locks are released from the tables.

Syntax:

ROLLBACK [TO [SAVEPOINT] <savepointname>];

SAVEPOINT is optional and is used to roll back a transaction partially.

savepointname is a savepoint ceated during the current transaction.

Creating a SAVEPOINT:

SAVEPOINT is used to mark and save the current point in the processing of a transaction. If a

SAVEPOINT is used with a ROLLBACK statement, parts of a transaction can be undone. An

Page 2: Pl sql-ch2

Prof. Mukesh N Tekwani [email protected]

2 Chap 2. PL/SQL Transactions and Cursors

active SAVEPOINT is one that is specified since the last COMMIT or ROLLBACK.

Syntax:

SAVEPOINT <savepointname>;

A ROLLBACK command can be given even without giving the SAVEPOINT clause.

If the ROLLBACK command is given without the SAVEPOINT option, the following take

place:

1. It ends the current transaction.

2. Undoes all the changes in the current transaction

3. Erases all the savepoints in that transaction

4. Releases the transactional locks.

If the ROLLBACK command is given with the TO SAVEPOINT option, the following take

place:

1. A predetermined portion of the transaction is rolled back

2. Retains the save point rolled to but loses those created after the savepoint.

3. Releases all transactional locks

3 Write a PL/SQL block of code that first withdraws an amount of Rs 1000. Then deposits

an amount of Rs 1,40,000. Update the current balance. Then check to see that the current

balance of all the accounts in the bank does not exceed 2,00,000. If the balance exceeds

2,00,000, then undo the deposit just made.

DECLARE

mBAL NUMBER(8, 2);

BEGIN

/* insert a record into TRANSMSTR for withdrawls */

INSERT INTO TRANSMSTR (TRANSNO, ACCTNO, DT, TYPE, TDETAILS,

DR_CR, AMT, BALANCE);

VALUES (‘T100’, ‘SB10’, ‘07-OCT-2010’, ‘W’, ‘Mobile Bill’, ‘W’, 1000, 30000);

/*Update the current balance of this account no, in the ACCTMSTR table */

UPDATE ACCTMSTR

SET CURBAL = CURBAL – 1000

WHERE ACCTNO = ‘SB10’;

/* Define a savepoint */

SAVEPOINT no_update;

/*insert a record into transactionmaster for deposits */

INSERT INTO TRANSMSTR (TRANSNO, ACCTNO, DT, TYPE, TDETAILS,

DR_CR, AMT, BALANCE);

VALUES (‘T101’, ‘SB10’, ‘07-OCT-2010’, ‘C’, ‘Deposit’, ‘D’, 14000, 171000);

/* update the current balance of account no SB10 in the ACCTMSTR table */

UPDATE ACCTMSTR SET CURBAL = CURBAL + 140000 WHERE ACCTNO =

‘SB10’;

/*store the total current balance from the ACCTMSTR table into a variable */

SELECT SUM(CURBAL) INTO mBAL from acctmstr;

Page 3: Pl sql-ch2

[email protected] Prof. Mukesh N. Tekwani

3 Chap 2. PL/SQL Transactions and Cursors

/* Now check if the current balance exceeds 200000 */

IF mBAL > 200000 THEN

/* undo the changes made to the transaction master table */

ROLLBACK TO SAVEPOINT no_update;

END IF;

/* make the changes permanent */

COMMIT;

END;

4 Explain the modes of processing PL/SQL block.

A PL/SQL block can be run in one of the two modes:

1) Batch processing mode in which records are gathered in a table and are manipulated at

regular intervals.

2) Real-time processing – in this, records are manipulated as they are created.

Batch processing is a PL/SQL block that is run at the SQL prompt at regular intervals to

process the table data. This can be done in Oracle using the technique called ‘cursors’.

Processing of SQL statements by Oracle:

Whenever an SQL statement is executed, Oracle engine performs the following tasks:

a) Reserves a private SQL area in memory.

b) Populates this area with data requested in the SQL sentence.

c) Processes the data in this memory area as required.

d) Frees the memory area when the processing of data is completed.

5 What is a cursor? State and explain the different types of cursors.

1. The Oracle engine uses a work area for its internal processing in order to execute an SQL

statement. This work area is private to SQL and is called a cursor.

2. The data that is stored in the cursor is called the active data set.

3. The size of the cursor in memory is the size required to hold the number of rows in the

active data set. But the actual size depends on the Oracle engine’s built in memory

management capabilities and also on the amount of RAM.

4. Oracle has a pre-defined area in main memory and cursors are opened in this area.

5. The values retrieved from a table are held in a cursor in memory by the Oracle Engine. Thi

data is transferred to the client over the network. A cursor is opened at the client end so

that data can be held at the client end.

6. If the number of rows returned by the Oracle engine is more than the area available in the

cursor at the client end, the cursor data and the retrieved data are swapped between the

operating system’s swap area and RAM.

7. When a cursor is loaded with multiple rows, the Oracle engine opens and maintains a row

pointer. This row pointer will be relocated within the cursor area if the user changes the

view.

8. Oracle also maintains multiple cursor variables which indicate the status of processing

being done by the cursor.

TYPES OF CURSORS:

Oracle uses two types of cursors: these are called the implicit cursor and the explicit cursor. If

the Oracle engine opens a cursor for its own internal processing, it is known as the implicit

cursor. A cursor can also be opened on demand and such a cursor is called an explicit cursor.

Page 4: Pl sql-ch2

Prof. Mukesh N Tekwani [email protected]

4 Chap 2. PL/SQL Transactions and Cursors

Attributes of Cursors:

1. When the Oracle engine creates an implicit or explicit cursor, it also creates cursor control

variables.

2. These variables are used to control the execution of the cursor.

3. There are four system variables that keep track of the current status of the cursor. These

cursor variables can be used in a PL/SQL block.

4. The four attributes of all cursors are:

Attribute Name Description

%ISOPEN Returns TRUE if the cursor is open, FALSE otherwise

%FOUND Returns TRUE if record was fetched successfully, FALSE

otherwise

%NOTFOUND Returns TRUE if record was not fetched successfully, FALSE

otherwise

%ROWCOUNT Returns number of records processed from the cursor

6 Write a note on Implicit cursor.

1. The Oracle Engine implicitly opens a cursor on the server to process each SQL statement.

2. This cursor is opened and managed by the Oracle engine internally and therefore the

functions of reserving an area in memory, populating this area with proper data, processing

the data in memory, and releasing the memory when the processing is complete, is taken

care of by the Oracle engine.

3. The resultant (processed) data is then passed to the client through the network.

4. A cursor is now opened in the memory of the client machine to hold the rows returned by

the Oracle engine. The number of rows held in the cursor on the client machine is managed

by the client’s OS and its memory area.

5. Implicit cursor attributes: These can be used to get information about the status of the last

INSERT, UPDATE, DELETE or single row SELECT statements.

6. The values of the cursor attributes always refer to the most recently executed SQL

statement.

7. If an attribute value is to be saved for later use, it must be assigned to a boolean memory

variable.

8. The implicit cursor attributes are as follows:

Attribute Name Description

%ISOPEN The Oracle engine automatically opens and closes the SQL

cursor after executing the INSERT, DELETE, SELECT, or

UPDATE operations. Thus, the SQL%ISOPEN attribute of an

implicit cursor cannot be referenced outside of its SQL

statement. Hence, SQL%ISOPEN always evaluates to FALSE.

%FOUND This evaluates to TRUE if an INSERT, UPDATE, or DELETE

affected one or more rows, or a single-row SELECT returned

one or more rows. Otherwise it evaluates to FALSE. This

attribute can be accessed as follows: SQL%FOUND.

%NOTFOUND This is the logical opposite of the %FOUND attribute. It

evaluates to TRUE if the INSERT, UPDATE or DELETE did

not any rows. Otherwise it evaluates to FALSE.

%ROWCOUNT Returns number of records affected by an INSERT, UPDATE,

or DELETE statements. Syntax is : SQL%ROWCOUNT

Page 5: Pl sql-ch2

[email protected] Prof. Mukesh N. Tekwani

5 Chap 2. PL/SQL Transactions and Cursors

Example 1: A bank manager has decided to transfer employees across branches. Write

PL/SQL code to accept an employee number and the branch number followed by updating the

branch number of that employee to which he belongs. Display an appropriate message using

SQL%NOTFOUND based on the non-existence of the record in the EMP_MSTR table.

BEGIN

UPDATE EMP_MSTR SET BRANCH_NO = &BRANCH_NO

WHERE EMPNO = &EMPNO

IF SQL%FOUND THEN

DBMS_OUTPUT.PUT_LINE (“Employee successfully transferred”);

END IF;

IF SQL%NOTFOUND THEN

DBMS_OUTPUT.PUT_LINE (“Employee number does not exist”);

END IF;

END;

Example 2:

The bank manager of Bandra branch decides to activate all those accounts which were

previously marked as inactive for performing no transactions in the last 365 days. Write a

PL/SQL block to update the status of accounts. Display an appropriate message based on the

number of rows affected by the update fired.

DECLARE

Rows_Affected char(4);

BEGIN

UPDATE ACCT_MSTR SET STATUS = ‘A’ WHERE STATUS = ‘S’ AND

BRANCH_NO IN (SELECT BRANCH_NO FROM BRANCH_MSTR WHERE

NAME = ‘Bandra’);

Rows_Affected := TO_CHAR(SQL%ROWCOUNT);

IF SQL%ROWCOUNT > 0 THEN

DBMS_OUTPUT.PUT_LINE(Rows_Affected || ‘accounts activated

successfully’);

ELSE

DBMS_OUTPUT.PUT_LINE(‘Currently there are no inactive accounts in the

Bandra branch’);

END IF;

END;

Example 3: In this code, we use implicit cursors to display a message stating whether or not

employee’s salaries were updated.

DECLARE

var_rows number(5);

BEGIN

UPDATE employee

SET salary = salary + 1000;

IF SQL%NOTFOUND THEN

dbms_output.put_line('None of the salaries where updated');

Page 6: Pl sql-ch2

Prof. Mukesh N Tekwani [email protected]

6 Chap 2. PL/SQL Transactions and Cursors

ELSIF SQL%FOUND THEN

var_rows := SQL%ROWCOUNT;

dbms_output.put_line('Salaries for ' || var_rows || 'employees are updated');

END IF;

END;

7 Write a note on Explicit cursor.

1. Explicit cursors must be created when you are executing a SELECT statement that returns

more than one row. Even though the cursor stores multiple rows (or records), only one row

can be processed at a time and this row is called the current row.

2. When individual records in a table have to be processed inside a PL/SQL code block, a

cursor is used. This cursor will be declared and mapped to a SQL query in the DECLARE

section of the PL/SQL block and used within the executable section. A cursor created in

this way is called an explicit cursor.

3. Steps involved in using an explicit cursor:

a. Declare a cursor mapped to a SQL SELECT statement that retrieves data for

processing.

b. Open the cursor.

c. Fetch data from the cursor, one row at a time into memory variables.

d. Process the data held in the memory variables as required using a loop.

e. Exit from the loop after processing is completed.

f. Close the cursor.

4. Cursor Declaration: A cursor is define din the DECLARE section of the PL/SQL code

block. This is done by naming the cursor and mapping it to a query. When a cursor is

declared, the Oracle engine is informed that a cursor needs to be opened. This declaration

does not allocate memory. The three commands used to control the explicit cursor are :

open, fetch and close.

5. Working of the Open, Fetch and Close commands:

a. The initialization of a cursor takes place through the open statement. This

statement defines a private area named after the cursor name. It also executes the

query associated with the cursor, retrieves the table and populates the area in

memory i.e., it creates the Active Data Set. It also sets the cursor row pointer to the

first record in the Active data Set. Syntax: OPEN cursorname;

b. The fetch statement then moves the data held in the Active Data Set into memory

variables. The fetch statement is placed inside a LOOP…END LOOP structure and

through this data can be fetched into memory variables and processed until all rows

in the Active Data Set are processed. The fetch loop then exits. Syntax: FETCH

cursorname INTO var1, var2, …Note that there must be a variable name for each

column value of the active data set and the data types must match. These variables

used for the fetch statement are declared in the DECLARE section.

c. After the fetch loop exits, the cursor must be closed with the close statement. This

will release the memory occupied by the cursor and its Active Data Set both on the

client and the server. Syntax: CLOSE cursorname. Once a cursor is closed, the

reopen statement is used to reopen the cursor.

6. Syntax of declaring cursor is: CURSOR cursorname IS SELECT statement;

7. Explicit cursor attributes: The four attributes are similar to those used for implicit cursor.

The attributes can be used in PL/SQL block for processing of data or for exiting. The

cursor name is appended to the attribute name when referring to an attribute.

8. The general format of using an explicit cursor is as follows:

Page 7: Pl sql-ch2

[email protected] Prof. Mukesh N. Tekwani

7 Chap 2. PL/SQL Transactions and Cursors

DECLARE

variables;

create a cursor;

BEGIN

OPEN cursor;

FETCH cursor;

process the records;

CLOSE cursor;

END;

Example 1: The bank manager has decided to mark all those accounts as inactive (I) on which

there are no transactions performed in the last 365 days. Whenever any such update takes

place, a record for the same is maintained in the INACTIVE_MSTR table which contains the

account number, the opening date and the type of account. Write a PL/SQL block to implement

this.

We first create the INACTIVE_MSTR table as follows:

CREATE TABLE INACTIVE_MSTR

(ACCTNO VARCHAR2(10),

OPENDT DATE,

TYPE VARCHAR2(2));

Now we write the PL/SQL code block:

DECLARE

CURSOR Crsr_NoTrans IS

SELECT ACCTNO, STATUS, OPENDT, TYPE FROM ACCT_MSTR

WHERE ACCTNO IN (SELECT ACCTNO FROM TRANS_MSTR

GROUP BY ACCTNO HAVING MAX(SYSDATE – OPENDT) > 365;

/* declare memory variables to hold data fetched from the cursor */

str_ACCTNO ACCTMSTR.ACCTNO%TYPE;

str_STATUS ACCTMSTR.STATUS%TYPE;

dt_OPENDT ACCTMSTR.OPENDT%TYPE;

str_TYPE ACCTMSTR.TYPE%TYPE;

BEGIN

OPEN Crsr_NoTrans;

/* if the cursor is open, continue with the data processing else display an appropriate

error message */

IF Crsr_NoTrans%ISOPEN THEN

LOOP

FETCH Crsr_NoTrans INTO str_ACCTNO, str_STATUS, dt_OPENDT,

str_TYPE;

EXIT WHEN Crsr_NoTrans%NOTFOUND;

IF Crsr_NoTrans%FOUND THEN

/*update status to ‘I’ and insert each record into the INACTIVE_MSTR

table */

UPDATE ACCTMSTR SET STATUS = ‘I’ WHERE ACCTNO =

Page 8: Pl sql-ch2

Prof. Mukesh N Tekwani [email protected]

8 Chap 2. PL/SQL Transactions and Cursors

str_ACCTNO;

INSERT INTO INACTIVE_MSTR VALUE (str_ACCTNO,

dt_opendt, str_TYPE);

END IF;

END LOOP;

/*make the changes permanent */

COMMIT

ELSE

Dbms_output.put_line (‘Error: Unable to open cursor’);

END IF;

CLOSE Crsr_NoTrans;

END;

In the above example, the %TYPE means that the data type of these variables will be as that

from the table.

Since the query may return more than one row, a loop is created which does the following:

(i) Fetch the data retrieved by the cursor into the variable.

(ii) Check if any data is retrieved

a. Update the status field of the AACT_MSTR table to indicate that the account is

inactive.

b. Insert a record in the INACTIVE_MSTR table

(iii) Repeat the above steps until the data retrieval process is completed.

(iv) Finally, the COMMIT statement is executed to make the changes permanent.

Example 2: Write a PL/SQL block that will display the customer name, the fixed deposit

number and the fixed deposit amount of the first 5 customers holding the highest amount in

fixed deposits.

DECLARE

CURSOR Crsr_HiFD IS

SELECT Fname || ‘ ‘ || LNAME, FDNO, AMT

FROM CUST_MSTR C, ACCT_FD_CUST_DETAILS A, FD_DETAILS F

WHERE C.CUSTNO = A.CUSTNO AND A.FDNO = F.FDSRNO

ORDER BY AMT Desc;

str_NAME VARCHAR2(40);

str_FDNO FD_DETAILS.FDNO%type;

num_AMT FD_DETAILS.AMT%type;

BEGIN

OPEN Crsr_HiFD;

dbms_output.put_line (‘Name FD No. Amount’);

dbms_output.put_line (‘-------------------------------------------------------------’);

LOOP

FETCH Crsr_HiFD INTO str_NAME, str_FDNO, num_AMT;

EXIT WHEN (Crscr_HiFD%ROWCOUNT-1) = 5 OR

Crscr_HiFD%NOTFOUND;

dbms_output.put_line (str_NAME || ‘ ‘ || str_FDNO || ‘ ‘ || num_AMT);

Page 9: Pl sql-ch2

[email protected] Prof. Mukesh N. Tekwani

9 Chap 2. PL/SQL Transactions and Cursors

END LOOP;

END;

To retrieve the records, both the tables have to be linked on the basis of common columns

using join as follows:

• C.CUSTNO = A.CUSTNO; this means the CUSTNO field of CUSTMSTR table is

joined with the CUSTNO field of ACCT_FD_CUST_DETAILS table.

8 Explain Cursor For Loops in PL/SQL.

The FOR loop is used in PL/SQL to control the looping in PL/SQL block.

The general syntax of this construct is :

FOR memoryvariable IN cursorname

The keyword FOR automatically creates the memory variable of the %rowtype. Each record

in the opened cursor becomes a value for the memory variable of the %rowtype. We also do

not open, fetch and close the cursor because these functions are done by the FOR statement

automatically.

Thus, the cursor FOR loop does the following automatically:

1. Implicitly declares its loop index as a %rowtype record.

2. Opens a cursor.

3. Fetches a row from the cursor for each loop iteration.

4. Closes the cursor when all the rows have been processed.

The general syntax for using FOR LOOP is :

FOR record_name IN cusror_name

LOOP

process the row...

END LOOP;

Example 1:

DECLARE

CURSOR emp_cur IS

SELECT first_name, last_name, salary FROM emp_tbl;

emp_rec emp_cur%rowtype;

BEGIN

FOR emp_rec IN sales_cur

LOOP

dbms_output.put_line(emp_cur.first_name || ' ' ||emp_cur.last_name || ' '

||emp_cur.salary);

END LOOP;

END;

In the above example, when the FOR loop is processed a record ‘emp_rec’ of structure

‘emp_cur’ gets created, the cursor is opened, the rows are fetched to the record ‘emp_rec’ and

the cursor is closed after the last row is processed. By using FOR Loop in our program, we can

reduce the number of lines in the program.

Page 10: Pl sql-ch2

Prof. Mukesh N Tekwani [email protected]

10 Chap 2. PL/SQL Transactions and Cursors

Example 2: The bank manager has decided to marks all those accounts as inactive (I) on

which there are no transactions performed in the last 365 days. Whenever any such update

takes place, a record for the same is maintained in the INACTIVE_MSTR table. This table

consists of the fields account number, opening date and type of account. Write PL/SQL code

block to do the same.

First we create the INACTIVE_MSTR table.

CREATE TABLE IANCTIVE_MSTR (

ACCTNO VARCHAR2(10),

OPENDT DATE,

ACCTYPE VARCHAR2(2));

Now we write the PL/SQL code block:

DECLARE

CURSOR Crsr_NoTrans IS

SELECT ACCTNO, STATUS, OPENDT, ACCTYPE FROM ACCTMSTR

WHERE ACCTNO IN (SELECT ACCTNO FROM TRANSMSTR

GROUP BY ACCTNO

HAVING MAX(SYSDATE – OPENDT) > 365;

BEGIN

/* we use the cursor FOR loop */

FOR NoTransRec IN Crsr_NoTrans

/* update the inactive status and insert each record into the INACTIVE_MSTR table */

UPDATE ACCT_MSTR SET STATUS = ‘I’

WHERE ACCTNO= NoTransRec.ACCTNO;

INSERT INTO INACTIVE_MSTR VALUES(NoTransRec.ACCTNO, NoTransRec.OPENDATE, NoTransRec.ACCTYPE);

END LOOP;

COMMIT;

END;

Since a FOR loop is used in this example, we did not declare any variables. The cursor closes

automatically when all the records have been processed. This is because there are no more

records left to load into NoTransRec. Finally, the COMMIT statement is fired to make the

changes permanent.

9 What are parameterized cursors?

Parameters provide a way to pass information into and out of a module. PL/SQL allows us to

pass parameters into a cursor.

The reasons for using parameterized cursor are:

• A parameter makes the cursor more reusable. Instead of hardcoding a value into the

WHERE clause of a query to select particular information, we can use a parameter and

then pass different values to the WHERE clause each time a cursor is opened.

• A parameter avoids scoping problems. When we pass parameters instead of hardcoding

values, the result set for that cursor is not tied to a specific variable in a program or

block. If the program has nested blocks, we can define the cursor at a higher-level

(enclosing) block and use it in any of the sub-blocks with variables defined in those

Page 11: Pl sql-ch2

[email protected] Prof. Mukesh N. Tekwani

11 Chap 2. PL/SQL Transactions and Cursors

local blocks.

Most commercial applications will require that the query which defines the cursor, be general

in nature and the data that is retrieved from the table be allowed to change according to the

users need. Oracle permits the use of parameterized cursor. The contents of the parameterized

cursor will change depending upon the value passed to its parameter.

Since the cursor accepts user0-defined values into its parameters, thus changing the result set

extracted from the table, it is called as a parameterized cursor.

Declaring a parameterized cursor:

CURSOR cursorname (varname datatype) IS <SELECT statement…>

Opening a parameterized cursor and passing values to it:

OPEN cursorname (vale/variable/expression)

The scope of the cursor parameter is confined to that cursor. You cannot refer to the cursor

parameter outside of the SELECT statement associated with the cursor.

Example 1:

Write a PL/SQL code block that will merge the data available in the newly created table

NEW_BRANCHES with the data available in the table BRANCH_MASTER. If the data in the

first table already exists in the second table, then that data should be skipped.

CREATE TABLE NEW_BRANCHES(

BRANCHNO VARCHAR2(10),

NAME VARCHAR2(30));

INSERT INTO NEW_BRANCHES (BRANCHNO, NAME) VALUES (‘B4’, ‘MAHIM’);

INSERT INTO NEW_BRANCHES (BRANCHNO, NAME) VALUES (‘B5’, ‘MATUNGA’);

INSERT INTO NEW_BRANCHES (BRANCHNO, NAME) VALUES (‘B6’, ‘DADAR’);

INSERT INTO NEW_BRANCHES (BRANCHNO, NAME) VALUES (‘B7’, ‘KHAR’);

Now WE WRITE THE PL/SQL CODE BLOCK:

DECLARE

/* This cursor retrieves all records of NEW_BRANCHES table */

CURSOR Crsr_Newbranches (str_BRANCHNAME VARCHAR2) IS

SELECT * FROM NEW_BRANCHES;

/* this cursor accepts the value of NAME from the current row of the cursor Crsr_Newbranches */

CURSOR Crsr_Branchchk (str_BRANCHNAME varchar2) IS

SELECT BRANCHNO FROM BRANCHMSTR

WHERE NAME = str_BRANCHNAME;

/* we create variables that hold the data from the cursor Crsr_Newbranches */

str_BRANCHNO BRANCH_MSTR.BRANCHNO.%TYPE;

str_BRANCHNNAME BRANCH_MSTR.NAME.%TYPE;

/* we create a variable that holds data from the cursor Crsr_Branchchk */

temp VARCHAR2(10);

Page 12: Pl sql-ch2

Prof. Mukesh N Tekwani [email protected]

12 Chap 2. PL/SQL Transactions and Cursors

BEGIN

/* open the Crsr_Newbranches cursor */

OPEN Crsr_Newbranches;

LOOP

/* fetch the records from the Crsr_Newbranches cursor */

FETCH Crsr_Newbranches INTO str_BRANCHNO, str_BRANCHNAME;

EXIT WHEN Crsr_Newbranches%NOTFOUND;

/* open the Crsr_Branchchk cursor. Note that the value of variable passed to the

CrsrBranchchk cursor is set to the value of NAME in the current row of the cursor

Crsr_Newbranches */

OPEN Crsr_Branchchk(str_BRANCHNAME);

FETCH Crsr_Branchchk INTO temp;

IF Crsr_BRANCHCHK%FOUND THEN

dbms_output.put_line (‘Branch ‘ || str_BRANCHNAME || ‘exists’);

ELSE

dbms_output.put_line (‘Branch ‘ || str_BRANCHNAME || ‘does not exist. Inserting

new branch in database’);

INSERT INTO BRANCH_MSTR VALUES (str_BRANCHNO,

str_BRANCHNAME);

END IF;

CLOSE Crsr_BRANCHCHK;

END LOOP;

CLOSE Crsr_Newbranches;

COMMIT;

END;