0% found this document useful (0 votes)
38 views5 pages

Declare Varchar2 Varchar2 Begin Select Into From Where Case When Then When Then When Then Else End End

The document contains examples of PL/SQL code demonstrating the use of IF/ELSE statements, CASE statements, loops (basic, while, for), GOTO statements, cursors, and fetching data from a cursor one row at a time. It shows how to conditionally execute code based on different conditions, iterate through loops, jump to labels using GOTO, declare and use cursors to retrieve data from a database table, and fetch each row of a cursor individually.

Uploaded by

Nasiruddin Sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

Declare Varchar2 Varchar2 Begin Select Into From Where Case When Then When Then When Then Else End End

The document contains examples of PL/SQL code demonstrating the use of IF/ELSE statements, CASE statements, loops (basic, while, for), GOTO statements, cursors, and fetching data from a cursor one row at a time. It shows how to conditionally execute code based on different conditions, iterate through loops, jump to labels using GOTO, declare and use cursors to retrieve data from a database table, and fetch each row of a cursor individually.

Uploaded by

Nasiruddin Sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

IF else:======================

SQL> declare
2 v_var1 employees.last_name%type;
3 begin
4
select last_name into v_var1 from employees where
employee_id='&enter_emp_num';
5
if(length(v_var1)<=5) then
6
dbms_output.put_line('You have very short name');
7
elsif (length(v_var1)>5 and length(v_var1)<=10) then
8
dbms_output.put_line('You have some what medium length name');
9
else if(length(v_var1)>10 and length(v_var1)<=20) then
10
dbms_output.put_line('you are having a large name');
11
else
12
dbms_output.put_line('there is no range provided for the current
empployee');
13
end if;
14
end if;
15
end;
18 /
You have very short name
PL/SQL procedure successfully completed
CASE=============================
declare
v_var1 varchar2(10);
v_var2 varchar2(40);
begin
select job_id into v_var1 from employees where
employee_id='&enter_emp_number';
v_var2:=case v_var1 when 'PU_CLERK' then 'you have 5 collegues'
when 'ST_MAN' then 'you have 5 collegues'
when 'ST_CLERK' then 'you have 20 collegues'
else 'your job position is not known to me'
end;
dbms_output.put_line(v_var2);
end;

CASE===============================================
===
declare

v_var1 varchar2(10);
v_var2 varchar2(40);
begin
select job_id into v_var1 from employees where
employee_id='&enter_emp_number';
v_var2:=case when v_var1 ='PU_CLERK' then 'you have 5 collegues'
when v_var1 ='ST_MAN' then 'you have 5 collegues'
when v_var1 ='ST_CLERK' then 'you have 20
collegues'
else 'your job position is not known to me'
end;
dbms_output.put_line(v_var2);

end;
BASIC LOOP==================================
declare v_var1 number;
begin
v_var1:=1;
loop
dbms_output.put_line(v_var1);
v_var1:=v_var1+1;
exit when v_var1>10;
end loop;
end;
basic loop to insert into a table =============================
declare
v_var1 number;
v_var2 number;
begin
v_var1:=1;
v_var2:=1;
loop
insert into sample_nsk values(v_var1,to_char(to_date(v_var2, 'j'),
'jsp' ));
v_var1:=v_var1+1;
v_var2:=v_var2+1;
exit when v_var2=28;
end loop;
end;
select * from sample_nsk;

Can not getting expected results..


Pls. Help

declare
v_var1 number;
v_var2 number;
v_var3 number;
begin
v_var2:=1;
v_var3:=100;
select commission_pct into v_var1 from employees where employee_id=100;
while (v_var1 is not null)
loop
v_var2:=v_var2+1;
select commission_pct into v_var1 from employees

where employee_id=v_var3;

v_var3:=v_var3+1;

end loop;
dbms_output.put_line('the '||v_var2||' employees have commission');
end;
output=======
the 4 employees have commission
PL/SQL procedure successfully completed
For loop to print 1 to 10 numbers==========
begin

for v_var1 in 1..10 loop


dbms_output.put_line(v_var1);
end loop;
end;

for loop to print first two record in a table================


SQL> declare
2
v_var1 employees%rowtype;
3
v_var2 number;
4
begin
5
v_var2:=100;
6
for v_for in 1..2 loop
7
select * into v_var1 from employees where employee_id
in(v_var2);
8
dbms_output.put_line(v_var1.last_name);
9
v_var2:=v_var2+1;
10
end loop;
11
end;
14 /
King
Kochhar
THE USAGE OF GOTO=========================================================
PL/SQL procedure successfully completed
SQL> begin
2
dbms_output.put_line('after begin and before calling
goto');
3
goto label1;
4
dbms_output.put_line(q'[This can't be printed]');
5
<<label1>>
6
dbms_output.put_line('after calling goto, the control
came to this point');
7
end;
8 /
after begin and before calling goto
after calling goto, the control came to this point

PL/SQL procedure successfully completed

CURSORS BASIC SYNTAX===========================


SQL> declare
2
cursor nsk_cur is select last_name, hire_date from
employees where employee_id in(100, 102, 103, 104, 105, 106, 107);
3
v_var1 date;
4
v_var2 varchar2(22);
5
begin
6
open nsk_cur;
7
for v_loop in 1..4 loop
8
fetch nsk_cur into v_var2, v_var1 ;
9
dbms_output.put_line(v_var2||' is joined in '||
v_var1||' Year ');
10
end loop;
11
close nsk_cur;
12
end;
13
14
15 /
King is joined in 17-JUN-87 Year
De Haan is joined in 13-JAN-93 Year
Hunold is joined in 03-JAN-90 Year
Ernst is joined in 21-MAY-91 Year
PL/SQL procedure successfully completed

CURSOR TOONE ROW AT A TIME======================================


SQL>
declare
2
cursor nsk_cur is select * from employees where
employee_id in(100, 102, 103, 104, 105, 106, 107);
3
v_var1 employees%rowtype;
4
v_var2 varchar2(22);
5
begin
6
open nsk_cur;
7
for v_loop in 1..4 loop
8
fetch nsk_cur into v_var1 ;
9
dbms_output.put_line(v_var1.last_name||' is joined
in '||to_char(v_var1.hire_date,'yyyy')||' Year ');
10
end loop;
11
close nsk_cur;
12
end;
13
14
15
16 /
King is joined in 1987 Year
De Haan is joined in 1993 Year
Hunold is joined in 1990 Year
Ernst is joined in 1991 Year

PL/SQL procedure successfully completed

You might also like