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);

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

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

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

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

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

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

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

The number of rows hold by a cursor is called active set. I. sql%row count: return count of rows effected by dml operations. sql%is open: always evaluates to false b’coz pl/sql closes implicit cursor immediately after they are executed.e. 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. we can process only one row at a time.15 where deptno=v_deptno. . 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. Returns true if no row affected by the dml operations. ex: declare v_deptno number(2):=&v_deptno. sql%notfound: it is quite opposite to previous./ CURSOR A cursor is a pl/sql object which is capable of storing more than one row at a time. begin update emp set sal=sal*1.

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

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

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

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

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

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

EXCEPTIONS: ----------An exception is raised if any statement violates RDBMS rules. an error occurred at runtime.e. An exception is a runtime error. 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.unnamed exceptions Named:-Each exception raised by oracle here an error number associated oracle have already named them such exceptions are called named exceptions. I.

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

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

put_line('give number').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. Now we can handle exception with the name associated. 3. dbms_output.put_line('sum='||C). End. / Note: .Begin C: ='&a'+'&b'. Ex:- .<error_number>).put_line('un known errors occurred'). 2. Exception When value_error then dbms_output. syn : pragma exception_init(<exception_name. Associate the created name to error number. When others then dbms_output. Create an exception name syn:<exception_name> exception.

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

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

. syn: create or replace procedure < par name.. 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. p2 dtype.procedures procedures:-.a procedure is a named pl/sql block which is saved in the database for later usage. / ..> is --------begin ------exception ------end.

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

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

dbms_output. dbms_output.put_line('dept name'||y). end. z varchar2(10). how to call procedure in pl/sql declare x number(2):=&x.loc into v_dname.:y) .y.z).put_line('dept loc:'||z). is begin select dname. v_dname out varchar2. v_loc out varchar2). y varchar2(10).:x. end.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. begin get_deptdata(x. / call procedure using bind variables sql> variable x varchar2(10) sql> variable y varchar2(20) sql> exec get_deptdata(30. exception when no_data_found then null.v_loc from dept where deptno=v_deptno.

inout argument: can be used for both input as well as output.v_dname varchar2. End. 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_loc varchar2:='newyark') is .sql> print :x sql> print :y sql>select :x. / Ways to pass parameters in procedure: Positional Conventional Combinational ex:Create or replace procedure insert_dapt(v_deptno number.:y from dual.

combinational (50. the difference is fun returns a values where as procedure can't returns a value.v_dname=>'marketing').v_dname.v_loc=>'boston'.'marketing'.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_deptno=>50. / positional: insert_dept(50.v_loc). end.'boston'). Functions:A function is also a named pl/sql block like a procedure.v_dname=>'marketing'). conventional: (v_loc=>'boston'.

. / select concat(ename.----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.job) from emp. / ex:create or replace function concat2(s1 varchar2. exception ------end. Return v_ename. 2. Begin Select ename into v_ename from emp where empno=v_empno.

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

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

/ 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. begin for r in c loop dbms_output(r. procedure dept_data is cursor c is select * from dept.sal).is procedure 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. end pack1. end loop. end emp_data. begin for r in c loop .

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

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

In trigger there are in two formats 1. raise_application_error(): to rise user defined errors./ Triggers A trigger is also a pl/sql block used to implement data integrity on tables. A trigger can be used to validate data and as well as to keep track Of existing data.'message'). . before 2. I. it can be Used to validate the data.e. a trigger get fires when ever an associated dml operation is Performed on a table.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. syx: raise_application_error(-errorno.

'DY')='SUN' THEN RAISE_APPLICATION_ERROR(-2000. to drop: drop trigger<trigger_name> Trigger will not allow commit and rollback statements.ttime varchar2(20)). Levels of triggers 1. 1. END IF.'CAN''T PERFORM ANY DML OPERATIONS ON SUNDAY'). END.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. Statement level trigger: it is a trigger when executed only once whenever DML operations per formed Create table emp_track(uname varchar2(10). Statement level trigger and 2.tdate date. Row level trigger First one is execute only for entire table Second one will execute for every row. CREATE OR REPLACE TRIGGER EMP_TRACK_TRIGGER .

ENAME).'HH:MM')).JOB:=UPPER(:NEW. TO_CHAR (SYSDATE.JOB). / Order of trigger execution . 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.AFTER INSERT OR UPDAT OR DELETE ON EMP BEGIN INSERT INTO EMP_TRACK VALUES (USER.ENAME:=UPPER(:NEW. END. / Row Level Triggers: it is a trigger which is executed for each row affected DML operation. SYSDATE. END. A row level trigger is used to validate data in each row. :NEW.

b'coz we can't perform DML or select on the same table in trigger body. To overcome this trigger mutating error we can implement the 'pragma autonomous_transaction'.procedure. note : the trigger . Alter trigger <trigger_name> enable or disable. 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 . 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.before statement level before row level after row level after statement level trigger.

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

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

'bujji')). type ref_cur is ref_cursor. .member('swati'.'kavya'.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). A ref cursor is used to associate a Select statement . 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. i. delete from table(select dependent from family_details where family_head='babu') where column_value='bujji'. syn: type <type_name> is ref cursor. sql> create table family_details(family_head varchar2(20).e we can associate a select statement to refcursor while opening the cursor. sql> select * from family_details.at runtime rather than at compile time.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. 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').

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

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

1..name:='madhu'. s sum_tbl. s(i). end loop. declare type sam_tbl is table of sample%rowtype index by binary_integer. set timing on. execute immediate str. . 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.put_line('rows deleted'||sql%rowcount).name varchar2(10)).bulk delete: can be implemented using 'bulk collection into'.1000 loop s(i).is str varchar2(100). begin str:='delete from'||tname. Create table sample(sno number. / bulk binding: binding reefers to executing an sql command in plsql block where there are may transactions to be performed from a plsql block.bulk insert: can be implemented using 'forall' 2. 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. begin for i in 1. dbms_output. end.

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

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

Sign up to vote on this title
UsefulNot useful