pl/sql

Programming language using sql. plsql block: a set of instructions runs in the database. to enhance capabilities of sql oracle introduced pl/sql in early 1990’s. a pl/sql is a combination of sql statements and as well programming constructs like Variables, if conditions, loop, control instructions etc. Features of pl-sql : Modular programming Security Exception handing pl-sql block : a block is a set of sql and plsql constructs which is executed as single unit. These are two types of blocks 1. Unnamed plsql block 2. Named plsql block. Unnamed plsql block: it is a plsql code which is not saved in the database but can be saved in client machine. Named blocks: stored in the database. ex: functions, procedures, packages, triggers. Declare (optional) Variable section Begin Executable section Exception (optional) End; Variable: a variable is a name gives to the memory location where a value is stored. syn:<variable><data type>[notnull :=value]

note: when it is not null give a value mandatory. ex: declare a number (3):=1000; b number (3):=1000; c number (3); c:= a+b; dbms_output.put_line(‘the sum of two numbers is||c); end; / constant variable: syn: <variable> constant<data type>:=value; ex: pi constant number (4, 2) =’3.14’; select …into: it must and should return only one row .if it returns more than one row or zero rows it is error. To retrieve row or string variables syn: select <column(s)> into <variables> from <table_name> [where <condition>]; ex: declare v_empno number (4):=&v_empno; v_ename varchar2 (10); begin select ename into v_ename from emp where empno=v_empno; dbms_output.put_line(‘employee name is’|| v_ename);

end; / declaring variables with existing columns data types <variable> <table.column>%type; v_ename emp.ename%type; v_job scott.emp.job%type; (if they are in different schema then). declare v_deptno dept.deptno%type; v_dname dept.dname%type; v_loc dept.loc%type; begin select * into v_deptno , v_dname ,v_loc from dept where deptno=&deptno; dbms_output.put_line(‘department number:’||v_deptno); end; record type: it is a collection of individual variables .it is a composite data type defined by user, also called as user defined data type. type <type_name > is record (variable 1 data type, variable2 data type,…); ex:- type emp_rec is record(ename varchar2(10), job varchar2(20),sal number(7,2)); to use a composite data type we should declare a variable and use it syn: <variable> <composite_data_type>; r emp_rec; r1 emp_rec; declare v_empno number (4):=v_empno; type emp_rec is record (ename varchar2 (10), job varchar2 (10), sal number (7, 2)); r emp_rec; begin

select ename,job,sal into r.ename,r.job,r.sal from emp where empno=v_empno; dbms_output.put_line(‘employee name:’||r.ename); dbms_output.put_line(‘employeejob:’||r.job); dbms_output.put_line(‘empsal:’||r.sal); end; / note: - above select …into statement using record variable can also be written as select ename,job,sal into r from emp where empno=v_empno; example 2 declare type dept_rec is record(deptno dept.deptno%type,dname dept.dname%type,loc dept.loc%typte); r dept_rec; begin select * into r from dept where deptno=&deptno; dbms_output.put_line(‘deptno:’||r.deptno); dbms_output.put_line(‘dname:’||r.dname); dbms_output.put_line(‘location at:’||r.loc); end; / declaring record variable based on existing table:syn:-<record_variable><table>%rowtype; declare r dept%rowtype; begin select * into r from dept where deptno=&deptno; dbms_output.put_line(‘deptno:’||r.deptno);

dname||’department’).put_line(‘employee’||r.put_line(‘location at:’||r. like if.put_line(‘dname:’||r.dept d where e. end.ename||’is working in’|| r.deptno and e.if…else. end.deptno=d.executes a set of statements if given condition is true. / do it: create a block which will accept department name and display no. syn: if<condtion> then statements.ename.loop control instructions decision control instructions: used to execute a set of statements based on a condition(variable).nested if .dbms_output.if…elsif.dname varchar2(10)). v_empno number(4):=&v_empno. dbms_output. dbms_output.of employees working in it control instructions: 1.d. / ex2:declare type emp_rec is record(ename varchar2(20). if:.dname into r from emp e. begin select e.decision control instructions 2.dname).empno=v_empno. r emp_rec.loc). .

if v_sal>=3000 then v_grade:=’a’. v_sal number(7. dmbs_output.put_line(‘employee grade is ‘||v_grade).end if. if v_sal <3000 then v_grade:-=’b’. end if. dbms_output. v_grade char(1):=’b’.2). ex:declare v_empno number(4):=v_empno. if v_sal >=3000 then v_grade:=’a’. end if. / declare v_empno number(4):=&v_empno.put_line(‘employee grade is ‘||v_grade). v_sal number(7.2). v_grade char(1). begin select sal into v_sal from emp where empno=v_empno. end . . begin select sal into v_sal from emp where empno:=v_empno. end if .

’president’. dbms_output. .’manager sal>3000- grade ‘a’ sal<3000- grade ‘b’ job not in ‘president’ manager sal>=3000 grade c sal<3000 grade d declare v_empno number(4):=v_empno. v_dname varchar2(10). v_count number. Basically plsql is combination of sql statements and programming language so on that situation both engines will work on switching process . / -declare v_dept number(2):=&v_deptno. If all clients are called at a time these are stored in queue based upon the priority it will take.put_line(v_deptno||v_dname||v__loc||v_count): end. nested if : using an if condition in another if is called nested if assign grade ‘a’ to employees who are working as job ‘analyst’.v_loc from dept where deptno=v_deptno. begin select dname. v_loc varchar2(10). that is called context switching. In sql the queries are stored client side only. select count(*) into v_count from emp where deptno=v_deptno.end if. note: in plsql procedures are stored in server these are called client system.loc into v_dname. These are take care in oracle server.

v_grade char. end if. if v_job in(‘president’.sal from emp where empno=v_empno. else v_grade :=’b’. end. else v_grade :=’b’. end if.v_job varchar2(10). / . begin select job. else v_grade:=’d’.put_line(‘gtade is’||v_grade). dbms_output. end if. else if v_sal>=3000 then v_grade:=’c’. v_sal number(7. ‘manager’) then if v_sal>=3000 then v_grade :=’a’. else if v_sal>=3000 then v_grade:=’a’.2). end if.

if sal>=5000 then grade:=’a’.if…elsif syn : if <condition > then action elsif <conditon> then ------------------else statements. endif.for loop . else grade:=’d’.while loop 3. elsif sal>=3000 then grade:=’c’.simple loop 2. elsif sal>=4000 then grade :=’b’. end if. loop control insturctions 1.

/ while: syx: while <conditon> loop statements. . end loop. end loop. statements.1.put_line(‘i’). exit when i=n. i:=i+1. n number:=&n. end.simple loop: syn: loop statements. Ex: declare 1 to n declare i number:=1. note: if condition is true then statement will exit. exit when<cond>. begin loop dbms_output.

begin while i<=n. loop dbms_output. FOR LOOP : syn: for <vari> in <val1> …<val2> loop statements. end loop. n number:=&n.end loop. ex:.. ex:-declare i number:=1. end. i: =i+1.put_line(i).put_line (i). end. begin for i in 1. end loop.n /* for i in reverse order*/ loop dbms_output.declare n number:=&n. end loop. .

ex: declare v_deptno number(2):=&v_deptno. sql%notfound: it is quite opposite to previous. I. begin update emp set sal=sal*1.e. sql%row count: return count of rows effected by dml operations. a cursor is temporary work area which can hold more than one row even though a cursor is capable of holding more than one row. The number of rows hold by a cursor is called active set. Returns true if no row affected by the dml operations. There are two types of cursors IMPLICIT CURSOR EXPLICIT CURSOR implicit cursor: An implicit cursor is the one which is raised by oracle when ever we perform dml operations when a select statement which returns only one row is executed using this implicit cursor we can know how many rows are effected by the dml operations to at least one row is effected or not attributes: sql%found true/false If it is true at least one row is affected by the dml operation. we can process only one row at a time. sql%is open: always evaluates to false b’coz pl/sql closes implicit cursor immediately after they are executed./ CURSOR A cursor is a pl/sql object which is capable of storing more than one row at a time. .15 where deptno=v_deptno.

close the cursor syn: close<curosr name>.location%type.put_line(‘no employees in dept’). begin open c.if sql%found then dbms_output.open the cursor syn: open <cursor name>. 4. . end. EXPLICIT CURSORS: An explicit cursor is the one which is created by user to hold more than one row . v_deptno dept.deptno%type.put_line(sql%rowcount||’emplouees updated’). ex:declare cursor c is select * from dept .fetch the records getting the rows from a cursor using fetch syn: fetch <cursor name> into <variable records>. elsif sql%notfound then dbms_output. 3. v_dname dept.a cursor may hold more than one row but we can process only one row at a time. end if . steps: 1 declare the cursor in the declaration session syx: cursor <cursor name> is <select statement>.dname%type. 2. v_loc dept.

dname from emp. / ex 2: declare cursor c is select ename. dbms_output. ex: declare cursor c is select * from dept. close c. dbms_output.v_loc .deptno||’ ‘||r.sal. .deptno=dept.v_dname. end. / in oracle fetch statement will follow forward approach where as in other data bases they follow both forward and backward. dbms_output.put_line(r. end. begin open c. fetch c into v_deptno.job.put_line(v_deptno||’ ‘||v_dname||’ ‘||v_loc).loc). dbms_output. note: consider if a table have four rows if i want to fetch the fifth row it will show null values. fetch c into r. close c. fetch c into r.v_loc .dname|| ‘ ‘||r.put_line(r.v_dname.fetch c into v_deptno.dname|| ‘ ‘||r. r dept%rowtype.deptno.loc).dept where emp.put_line(v_deptno||’ ‘||v_dname||’ ‘||v_loc).deptno||’ ‘||r.

put_line(r.sal||r. loop fetch c into r. . close c.ename||r.r c%rowtype. end. fetch c into r. <cursorname>%is open: return true if cursor is already open. r c%row type. begin open c. <cursorname>%not found: returns true if the fetch statement is not successful. / attributes: these are actually start with ‘cursor name’ <cursorname>%found: returns true if the fetch statement is successful.job||’ ‘||r.job||r. end loop. exit when c%not found. dbms_output. cursor with simple loop: declare cursor c is select ename. <cursorname>%row count: returns count of rows fetched up to now.job.put_lime(r. dbms_output. begin open c.sal).ename||’ ‘||r.sal from emp.dname).

close c.put_line(‘sno ||’ ‘||sname||’ ‘||sal grade’).cursor with while loop: declare cursor c is select ename. begin dbms_output.10)||rpad(r.dbms_output. / 2. / example 3: declare cursor c is select ename.sal from emp. end.sal.put_line(rpad(r.job. ferch c into r.job. loop dbms_output. end. close c. fetch c into r.sal from emp order by ename. end loop.job. while c%found.8)). begin open c.ename. for r in c . r c%rowtype.put_line(c%rowcount||’rows fetching’).10)||lpad(r. grade char.

declare v_deptno number:=&v_deptno. r emp%rowtype.sal>=5000 then grade:=’a’. ---------------------------------------------------------------------------------------------------------------------cursor with parameters (parameter cursor).10)||rpad(r.put_line(‘rpad(c%rowcount. else grade:=’d’. . elsif r. exit when c%notfound. endif.8)). A cursor can accept parameters and return rows based on the passed parameters.par2 datatype).ename. elsif r.loop if r. loop fetch c into r.job. end.4)||rpad(r.sal>=3000 then grade: =’c’. end loop.sal>=4000 then grade: =’b’. syn: cursor <name>(par1 datatype. cursor c(x number) is select * from emp where deptno=x.sal. dbms_output. begin open c( v_deptno).10)|| lpad(r.

close c. ----------------------------------------- . r emp%rowtype. end. begin for i in (select * from dept). close c.ename). end loop.dname). open c(i. end. loop fetch c into r.ename). end loop. ex 2: declare cursor c(x number) is select * from emp where deptno=x.dbms_output.deptno). dbms_output.put_line('dept name'||i. loop dbms_output.put_line(r. exit when c%notfound. end loop.put_line(r.

an error occurred at runtime.EXCEPTIONS: ----------An exception is raised if any statement violates RDBMS rules. I. An exception is a runtime error.e.unnamed exceptions Named:-Each exception raised by oracle here an error number associated oracle have already named them such exceptions are called named exceptions. Having named exceptions error no ora-01403 exception name no_data_found DESC when select into statement returns no rows ora-01422 ora-1001 ora_06511 too_many_ rows invalid_cursor cursor_ already_open when select into statement returns more than one row when we fetch rows from a cursor when is not yet open when we open a cursor which is already open ora-06502 value_error when converting char data to number . They are two types of exceptions 1. Named exceptions 2.

syn declare ----------------------begin statements. Begin a:=&a. c:=a/b. . exception when <exception_name> then --------------when <exception_name> then --------end. ex: Declare a number. c number. zero_devide exception. b number.. b:=&b.

dbms_output. v_job varchar2 (10). / Note: dbms_output.dbms_output. end.put_line('cannot divide a value by zero').put_line('number should be number farmate'). / Declare v_ename varchar2 (100).put_line('result is='||c). dbms_output.put_line('emp name:'||v_ename). Begin Select ename.put_line(sql errm). Exception When no_data_found then dbms_output. It will show the error message and sql code-> display error code 3) Declare C number.put_line('emp doesnot exist'). End.put_line('emp job:'||v_job).v_job from emp where empno='&empno'. .job into v_ename. When invalid_number then dbms_output. Exception when zero_devide then dbms_output.

Create an exception name syn:<exception_name> exception. Ex:- . 2.put_line('give number'). / Note: . syn : pragma exception_init(<exception_name.check built in fun for error message of error code Handling UN named exceptions: -------------------------To handle unnamed exceptions which is not named by oracle we should associate a name to the specific exception and handle we can associate a name to exception using 'pragma exception_init' Steps 1.put_line('un known errors occurred').Begin C: ='&a'+'&b'. 3.put_line('sum='||C). dbms_output.<error_number>). Now we can handle exception with the name associated. When others then dbms_output. Exception When value_error then dbms_output. End. Associate the created name to error number.

Else dbms_output. If sql%found then dbms_output.put_line('employees exist in this deparment').put_line('department deleted'). / ---Exception propagation: ---------------------If we write exceptions for one condition but there was raised exception Regarding to another condition. End if.-2292). Begin Delete from dept where deptno=v_deptno. Exception When child_data_found then dbms_output.put_line('dept does not found'). pragma exception_init(child_data_found. .Declare v_deptno number:=&v_deptno. End. child_data_found exception.

. / Modular programming: means the entire block should be placed in another begin block so we can handle exceptions in declare block. update emp set sal=new_sal where empno=v_empno.2). end. invalid_salary exception. Begin Select Sal into v_sal from emp where empno=&v_empno. raise<exception_name>. new_sal number(7.User defined exceptions: These are the exceptions which are raised by the user based on company or project requirement.2):=&new_sal. We can raise an exception using the statement like 'raise'.put_line('can't increase salary more than 20%'). if new_sal>v_sal*1. end if. Exception name be user defined or pre defined We may raise predefined exception name or user defined exception name Ex: Declare v_empno number(4):=&v_deptno.2 then raise invalid_salary. v_sal number(7. exception when invalid_salary then dbms_output.

note: don’t write substitution variables in procedures b'coz if we give the value for first from second time onwards it will store the same value.a procedure is a named pl/sql block which is saved in the database for later usage. syn: create or replace procedure < par name. / .procedures procedures:-.. p2 dtype.> is --------begin ------exception ------end...

put_line(r. close c.sal from emp. . r c%rowtype.ename||r.job. show error: to view errors in named block sql:show err note: procedure will not return a value.job. loop fetch c into r. / Execution process: syn: exec get_emp_data. dbms_output. Create or replace procedure get_emp_data(v_deptno number) is Cursor c is select ename.create or replace procedure get_emp_data is cursor c is select ename. 2. end loop.job||r.sal from emp where deptno=v_deptno. end. r c%rowtype.sal). begin open c. exit when c%not found.

sal).v_max from emp where upper(job)=upper(v_job).out parameters 3. begin select count(*).v_sum. / 3.of employees working as '||v_job||v_count). close c. dbms_output. dbms_output. v_sum number.job||r.put_line('no. create or replace procedure job_desc(v_job varchar2(20)) is v_count number.begin open c.ename||r. Type of parameters 1.sum(sal).inout parameters Procedure xyz(a number.max(sal) into v_count. v_max number. dbms_output. loop fetch c into r. end loop.put_line('total salaries paid||v_sum). end.In parameters 2.put_line(r. exit when c%not found.b number) .

begin get_deptdata(x. z varchar2(10).v_loc from dept where deptno=v_deptno.:x.z). is begin select dname.:y) .loc into v_dname. how to call procedure in pl/sql declare x number(2):=&x. end.put_line('dept name'||y). v_loc out varchar2). / call procedure using bind variables sql> variable x varchar2(10) sql> variable y varchar2(20) sql> exec get_deptdata(30.put_line('dept loc:'||z). dbms_output. end. v_dname out varchar2.y. y varchar2(10). exception when no_data_found then null.These are in parameters Out parameters: these are used to get output from a procedure Create or replace procedure get_deptdata(v_deptno in number. dbms_output.

This variable can be used for both ex Create or replace procedure get_emp_count(v_deptno inout number) Is Begin Select count (*) into v_deptno from emp where deptno=v_deptno.v_dname varchar2.:y from dual. End.sql> print :x sql> print :y sql>select :x. inout argument: can be used for both input as well as output.v_loc varchar2:='newyark') is . / Ways to pass parameters in procedure: Positional Conventional Combinational ex:Create or replace procedure insert_dapt(v_deptno number.

Functions:A function is also a named pl/sql block like a procedure.v_deptno=>50. end.v_dname=>'marketing').'boston').v_loc).begin insert into dept values(c_deptno. we can use these functions in a select statement also just like 'pre defined functions' Create or replace function <function_name>[(<parameters>)] return <return_type> is -----begin --- .v_dname. the difference is fun returns a values where as procedure can't returns a value. conventional: (v_loc=>'boston'. combinational (50. / positional: insert_dept(50.v_dname=>'marketing').'marketing'.v_loc=>'boston'.

/ ex:create or replace function concat2(s1 varchar2. / select concat(ename. Begin Select ename into v_ename from emp where empno=v_empno. . 2.----return<value>. Accept empno and return ename Create or replace function emp_ename(v_empno number) return varchar2 is v_ename varchar2(20). end.s2 varchar2) return varchar2 is begin return s1||'||s2. Return v_ename.job) from emp. exception ------end.

create or replace function get_count(v_deptno number) return number is v_count number. end.dname. begin select count(*) into v_count from emp where deptno=v_deptno. retunn v_count. cursors. constants. record types Other collection types.get_count(deptno) from dept. / select get_name(7902) from dual. . select deptno.exception when no_data_found then return 'none'. end. Packages:A package is a group of individual pl/sql constrains like variables. / select count(10) from dual. select get_name(empno) from emp. procedures and functions.

cursors .create or replace package<pack_name> is||as variable(s)||constants|| record type. record types And other collections types declared. procedures and function definitions.procedures.Package creation is involved in two steps Create package specification Create package body Package specification consists of variables . / body: Create or replace body<pack_name> is||as Cursor <cursor_name>returns <return_type> is Select statement. Procedure <procedure_name>[arguments] is ---- . end <pack_name>.functions . syn:. cursor(s) return<return type>. A package body which consists of cursor. collection type(s).constants .

/ ex: create or replace package pack1 . end <pack_name>.------begin --exception ------end <proc_name>. function <function_name>[(arguments)] return <return_type> is ------begin -------exception ------end <fun_name>.

ename||r. procedure dept_data.is procedure emp_data. end loop. procedure dept_data is cursor c is select * from dept. begin for r in c loop . end emp_data.job||r. Execute this Now create the body of the pack Create or replace package body pack1 is procedure emp_data is cursor c is select * from emp. begin for r in c loop dbms_output(r. end pack1.sal).

end pack1. / 2. v_manager varchar2(10). exception when no_data_found then . end loop.put_line(v. Create or replace package emp_pack is r emp%rowtype. end dept_data.dname||r. dbms_output. end emp_pack.loc). v_manager:=get_name(r.deptno||r.put_line(r.ename). procedure get_emp(v_empno number) is begin select * into r from emp where empno=v_empno. procedure get_all_emp(v_deptno number). dbms_output.dbms_output. procedure get_emp(v_empno number). / create or replace package body emp_pack is function get_name(v_empno number) return varchar2.mgr).manager).put_line(r.

procedure get_all_emp(v_deptno number) is cursor c is select * from emp where deptno=v_deptno. . begin open c. loop fetch c into r.put_line('emp does not support'). end emp_pack.ename||v_manager). end get_emp. begin select ename into v_ename from emp where empno=v_empno.dbms_output. function get_name(v_empno number) return varchar2 is v_ename varchar2(10). exit when c%notfound. end get_all_emp. end get_ename.put_line(r. v_manager:=get_name(r. close c. return v_ename. dbms_output. end loop.mgr). exception when no_data_found then return 'none'.

before 2.e. I.after Create or replace trigger<trigger_name> Before || after|| instead of Insert or update or delete of columns on <table_name> [referenceing old as o new as n] [for each row] [when <cond>] Declare Begin ------End. In trigger there are in two formats 1. raise_application_error(): to rise user defined errors. A trigger can be used to validate data and as well as to keep track Of existing data. .'message')./ Triggers A trigger is also a pl/sql block used to implement data integrity on tables. it can be Used to validate the data. syx: raise_application_error(-errorno. a trigger get fires when ever an associated dml operation is Performed on a table.

'CAN''T PERFORM ANY DML OPERATIONS ON SUNDAY').'DY')='SUN' THEN RAISE_APPLICATION_ERROR(-2000. 1. CREATE OR REPLACE TRIGGER EMP_TRACK_TRIGGER .tdate date.ttime varchar2(20)). to drop: drop trigger<trigger_name> Trigger will not allow commit and rollback statements. END. Statement level trigger and 2.the number should be -20000 to -20999 if it is Sunday no need to do any transactions CREATE OR REPLACE TRIGGER NO_TRANS_ON_SUN BEFORE INSERT OR UPDATE OR DELETE ON EMP BEGIN IF TO_CHAR(SYSDATE. Row level trigger First one is execute only for entire table Second one will execute for every row. Statement level trigger: it is a trigger when executed only once whenever DML operations per formed Create table emp_track(uname varchar2(10). Levels of triggers 1. END IF.

END.'HH:MM')). / Order of trigger execution .JOB:=UPPER(:NEW. SYSDATE.ENAME:=UPPER(:NEW. :NEW.JOB). END.ENAME). A row level trigger is used to validate data in each row. / Row Level Triggers: it is a trigger which is executed for each row affected DML operation. TO_CHAR (SYSDATE.AFTER INSERT OR UPDAT OR DELETE ON EMP BEGIN INSERT INTO EMP_TRACK VALUES (USER. We can validate data using the Qualifier old and new old contains old data and new contains new specification by adding For each row in trigger creation CREATE OR REPLACE TRIGGER TO_UPPDER BEFORE INSERT OR UPDATE ON EMP FOR EACH ROW BEGIN :NEW.

before statement level before row level after row level after statement level trigger. note : the trigger .b'coz we can't perform DML or select on the same table in trigger body. TRIGGER MUTATION : A trigger is said to be mutating when ever we try to perform dml operations or select operation in trigger body of the same table an exception is raised which is called trigger mutation error.procedure. To overcome this trigger mutating error we can implement the 'pragma autonomous_transaction'. functions or packages are stored in data dictionary user_procedures If you want to know code the user_sources trigger cascade: one trigger should not call another trigger NOTE: Instead of is used when you are going for VIEWS . Alter trigger <trigger_name> enable or disable.

if we not declare index by option and data type then it follow linked list ex: type< name> is table of varchar2(10) index by binary_integer. a plsql table is one of the collection type. loop fetch c into v_ename. n(idx):=v_ename. cursor c is select ename from emp. . exit when c%notfound. begin open c. ex: type names is table of varchar2(10) index by binary_integer. v_ename varchar2(10).COLLECTIONS plsql tables: A table type is used to show more then one row of values . syn:. So the use plsql table we should declare a variable to it and use it.type <type_name> is table of <datatype> ex: type <type_name> is table of <datatype> [index by binary_integer/pls_integer]. idx number:=0. idx:=idx+1. n number. n names.

exists(5).delete -all the indexes deleted present in table.put_line(n(i)). end loop. data. prior: privious index count: to know number of indexes present in a table delete to delete all indexes or specified index data.prior(6). data.first last: return the last index number. Extend: to extend an index of a non indexed table here it is extend one place for one time.delete(5).false b'coz it is already deleted.delete(idxno) delete specified ex data. . exists:returns true if specific index exists data. o/p:4 b.next(5)->after the 5th what we have.end loop.idx loop dbms_output.. next: next index number of a specified index data. table type attributes: first : returns first index number of a table ex: data.coz 5th is deleted. for i in 1. end.

syn: type <type_name> is ref cursor.dependents members) Nested table dependents store as dept_tbl / Note: dept_tbl is new table which stores family_head insert into family_details values('babu'. c ref_cur.e we can associate a select statement to refcursor while opening the cursor. sql> select * from family_details. delete from table(select dependent from family_details where family_head='babu') where column_value='bujji'.'bujji')). sql> select * from table(select dependent from family_details where family_head='babu') insertion : insert into table(select dependent from family_details where family_head='babu') values ('ramesh'). sql> create table family_details(family_head varchar2(20).member('swati'.Nested tables: while creating a table in sql if we take any of the column data type has a pl/sql table type then it is called nested tables sql> create type members is table of varchar2(15). i.at runtime rather than at compile time. . type ref_cur is ref_cursor.'kavya'. NOTE: Here dept_tbl is storage table if we want to access data by original table note: oracle do not follow pointers thats only we are using dependents store concept Refcursor: it is basically a data type. A ref cursor is used to associate a Select statement .

END LOOP. open c for select * from dept. R2 DEPT%ROWTYPE. C REF_CUR. LOOP FETCH C INTO R1. CLOSE C.here c is called cursor variable open c for select * from emp. BEGIN OPEN C FOR SELECT * FROM EMP.ENAME). ------close c.PUT_LINE(R1. R1 EMP%ROWTYPE. DBMS_OUTPUT. EXIT WHEN C%NOTFOUND. . OPEN C FOR SELECT * FROM DEPT. ------close c. ex: DECLARE TYPE REF_CUR IS REF CURSOR.

EXIT WHEN C%NOTFOUND.PUT_LINE(R2. It can be done in two ways 1. End. END LOOP. To perform DDL cmd we can use dynamic sql. Dynamic sql: It is a string which contains any DDL or DML command with variables Such string should be executed as a command. END. Begin Execute immediate’ create table sample(name varchar2(10))’.DNAME). CLOSE C. End.LOOP FETCH C INTO R2. / EX2:- Create or replace procedure del_rows(tname varchar2) . DBMS_OUTPUT. Using native dynamic sql [execute immediate] Ex:Begin Create table sample (name varchar2 (10)). Using dbms_sql package 2. Error because we can’t execute DDL cmd is a plsql block.

s(i).bulk delete: can be implemented using 'bulk collection into'. dbms_output. 1. For each transaction a context switch occurs between plsql and sql engine for more transactions more switches occur which will increase the cpu time usage. Create table sample(sno number. begin str:='delete from'||tname. begin for i in 1. / bulk binding: binding reefers to executing an sql command in plsql block where there are may transactions to be performed from a plsql block.is str varchar2(100).bulk insert: can be implemented using 'forall' 2.name varchar2(10)). declare type sam_tbl is table of sample%rowtype index by binary_integer. set timing on. end loop. .1000 loop s(i).. end. execute immediate str. s sum_tbl. bulk binding is used to perform more transactions in a single switch to reduce cpu time usage which will increase the speed of execution there are two types of bulk binding.sno:=1.name:='madhu'.put_line('rows deleted'||sql%rowcount).

.s(i). s sam_tbl. end. / here no. end loop. exit when c%notfound.. idx:=idx+1. begin open c. loop fetch c into r. cursor c is select * from sample.1000 insert into sample values s(i). idx number:=0.of switches occurred to insert rows can be reduced to one switch using 'forall' as follows forall i in 1.sno. example2: declare type sam_tbl is table of sample%rowtype index by binary_integer.for i in 1..name).1000 loop insert into sample values(s(i). r sample%rowtype.

/ . index by binary_integer.s(idx):=r. using 'bulk collection into ' to reduce no of switches declare type sam_tbl is table of sample%rowtype. s sam_tbl. end. begin select * bulk collection into s from sample. end loop. close c. end c.

Sign up to vote on this title
UsefulNot useful