Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Oracle Based Tech

Centre of Excellence

Best Practices: PL SQL


Mahendra Sonawane
10 Aug 2011
2005 KPIT Cummins Infosystems Limited

We value our
relationship

Loops, Cursors

for i in reverse 1..50 loop

Creating a new Cursor


based on a Cursor.
Cursor c1 is select ...
C2 c1%rowtype;

s := i;
end loop;

SELECT INTO statement


performs two queries
Fetch data from database
Determines
TOO_MANY_ROWS
exception to be raised or
not.

For a single row sub query


Implicit cursor performs two
fetches
Explicit cursor performs single
fetch.

Version 1.0

Presenter: OB Mgmt

For update where current of cursor


For Update : is used to lock cursor fetched rows until commit

Normally when other user update rows fetched by cursor, then cursor equerries.
declare
cursor c1 is select * from emp for update of sal,comm;
begin
for c in c1 loop
update emp set sal = sal + 2000 where current of c1;
end loop;
commit;
end;

Version 1.0

Presenter: OB Mgmt

Cursor Loop & commit


If commit is inside the LOOP
It will raise error
ORA-01002: fetch out of sequence
ORA-06512: at line # This is called Fetching Across
Commits.
To mimic WHERE CURRENT OF
Use rowid column in select clause of cursor.
4

Version 1.0

Presenter: OB Mgmt

Exception , Mutating , sub query

Version 1.0

Presenter: OB Mgmt

Sub Query

Version 1.0

Presenter: OB Mgmt

Sub query and Join

Version 1.0

Presenter: OB Mgmt

Operators (Some , Any)

Version 1.0

Presenter: OB Mgmt

Set Operators

Version 1.0

Presenter: OB Mgmt

Operator precedence

10

Version 1.0

Presenter: OB Mgmt

Rules of precedence

11

Version 1.0

Presenter: OB Mgmt

Uses And , OR

12

Version 1.0

Presenter: OB Mgmt

Rownum

13

Version 1.0

Presenter: OB Mgmt

Global Temporary Table

14

Version 1.0

Presenter: OB Mgmt

Union , Union All, Count, Bulk Collect

15

Version 1.0

Presenter: OB Mgmt

Nocopy

16

Version 1.0

Presenter: OB Mgmt

Nocopy

17

Version 1.0

Presenter: OB Mgmt

Points to remember

18

Version 1.0

Presenter: OB Mgmt

Points to remember

19

Version 1.0

Presenter: OB Mgmt

Points to remember

20

Version 1.0

Presenter: OB Mgmt

Any Question
21

Version 1.0

Presenter: OB Mgmt

Thank You
22

Version 1.0

Presenter: OB Mgmt

You might also like