June 7th Thursday

3
1. We cannot change SQL statements associated to it. 2. We cannot pass explicit cursor as a parameter between subprograms. Write a PL/SQL block for displaying information from emp table for any where condition { select * from emp where &condition } In such situations we use REF Cursor or cursor variable or dynamic cursor: ----------------------------------------------------------- We can change the condition during the program execution. Using a ref cursor is a two step process. 1. Declare a variable of type REF cursor. Syntax: type <typename> is REF cursor [return <datatype>]; Ex: type a is ref cursor r eturn emp%rowtype; -- If we specify return t ype then it is called as strongly typed cursor or constrained cursor. type b is ref cursor; -- Week cursor or unconstrained cursor 2. Declare a variable of type declared in above step. Ex: x a; -- x is cursor variable of type a ... with return emp%rowtype. y b; -- y is cursor variable of t ype b 3. Open the step two variables for any select statement... Syntax: open <cursorvariable> for <selectstatement>; Ex: open x for 'select * from emp where deptno=10'; open y for 'select empno,ename,sal,comm from emp where job=''CLERK'''; 4. Process cursor variable similar to explicit cursor. Note: We cannot use cursor for loop for these ref cursors.

Transcript of June 7th Thursday

Page 1: June 7th Thursday

7/31/2019 June 7th Thursday

http://slidepdf.com/reader/full/june-7th-thursday 1/3

1. We cannot change SQL statements associated to it.

2. We cannot pass explicit cursor as a parameter between subprograms.

Write a PL/SQL block for displaying information from emp table for any where condition

{

select * from emp

where &condition

}

In such situations we use

REF Cursor or cursor variable or dynamic cursor:

-----------------------------------------------------------

We can change the condition during the program execution.

Using a ref cursor is a two step process.

1. Declare a variable of type REF cursor.Syntax:

type <typename> is REF cursor [return <datatype>];

Ex:

type a is ref cursor return emp%rowtype; -- If we specify return type then it is called as strongly typed

cursor or constrained cursor.

type b is ref cursor; -- Week cursor or unconstrained cursor

2. Declare a variable of type declared in above step.

Ex:

x a; -- x is cursor variable of type a ... with return emp%rowtype.

y b; -- y is cursor variable of type b

3. Open the step two variables for any select statement...

Syntax:

open <cursorvariable> for <selectstatement>;

Ex:

open x for 'select * from emp where deptno=10';

open y for 'select empno,ename,sal,comm from emp where job=''CLERK''';

4. Process cursor variable similar to explicit cursor.

Note: We cannot use cursor for loop for these ref cursors.

Page 2: June 7th Thursday

7/31/2019 June 7th Thursday

http://slidepdf.com/reader/full/june-7th-thursday 2/3

 

set serveroutput on

declare

type a is ref cursor;

x a;

condition varchar2(1000) := '&condition';

slstmt varchar2(1000);

i emp%rowtype;

begin

sqlstmt:= 'select * from where' || condition;

open x for sqlstmt; -- opening a cursor variable for sql statement

loop

fetch x into i;

exit when x%notfound;

dbms_output.put_line('Ename is ' || i.ename);

end loop;

close x;

end;

set serveroutput on

declare

x sys_refcursor; -- sys_refcursor is a builtin ref cursor variable

condition varchar2(1000) := '&condition';

slstmt varchar2(1000);

i emp%rowtype;

begin

sqlstmt:= 'select * from where' || condition;

open x for sqlstmt; -- opening a cursor variable for sql statement

loopfetch x into i;

exit when x%notfound;

dbms_output.put_line('Ename is ' || i.ename);

end loop;

close x;

end;

Write a PL/SQL block for displaying number of columns in a given table.

set serveroutput on

declare

type a is ref cursor;

x a;

col_count number(2);

slstmt varchar2(1000);

v_tab_name varchar2(32):= '&tabname';

begin

sqlstmt:='select count(*) from user_tab_column where table_name =' || v_tab_name;

Page 3: June 7th Thursday

7/31/2019 June 7th Thursday

http://slidepdf.com/reader/full/june-7th-thursday 3/3

open x for sqlstmt;

loop

fetch

-------------------

-------------------

-------------------

IMPLICIT CURSOR:

-----------------------

"SQL" -- DML (i/u/d)

SQL%ISOPEN -- FALSE

SQL%FOUND

SQL%NOTFOUND

SQL%ROWCOUNT -- Given the information about number of rows effected.

HW ASSIGN:

Write small notes about SQL%FOUND

SQL%NOTFOUND