0% found this document useful (0 votes)
28 views10 pages

Dbms File

The document discusses various PL/SQL concepts including declaring variables, executing DML statements, control structures like IF/ELSE, CASE, triggers, procedures, functions, and cursors. It provides code examples to demonstrate how to declare and assign variables, use control structures, create and call procedures and functions, create and use triggers, and work with implicit and explicit cursors in PL/SQL.

Uploaded by

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

Dbms File

The document discusses various PL/SQL concepts including declaring variables, executing DML statements, control structures like IF/ELSE, CASE, triggers, procedures, functions, and cursors. It provides code examples to demonstrate how to declare and assign variables, use control structures, create and call procedures and functions, create and use triggers, and work with implicit and explicit cursors in PL/SQL.

Uploaded by

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

PL/SQL

SQL> set serverout on;


SQL> Declare
message varchar2(20):='Hello World';
begin
dbms_output.put_line(message);
end;
/
OUTPUT
Hello World

PL/SQL procedure successfully completed.

SQL> Declare
var number (05);
begin
var:=100;
dbms_output.put_line('var='||var);
end;
/
OUTPUT
var=100

PL/SQL procedure successfully completed.

SQL> Declare
a number(10);
b number(10);
c number(10);
begin
a:= &a;
b:= &b;
c:= a+b;
dbms_output.put_line('Sum='||c);
end;
/
OUTPUT
Enter value for a: 5
old 6: a:= &a;
new 6: a:= 5;
Enter value for b: 8
old 7: b:= &b;
new 7: b:= 8;
Sum=13

PL/SQL procedure successfully completed.

SQL> Declare
a number(10);
b number(10);
c number(10);
begin
a:= &a;
b:= &b;
c:= a/b;
dbms_output.put_line('div='||c);
exception when others then
dbms_output.put_line('check value of b');
end;
/
OUTPUT
Enter value for a: 6
old 6: a:= &a;
new 6: a:= 6;
Enter value for b: 0
old 7: b:= &b;
new 7: b:= 0;
check value of b

PL/SQL procedure successfully completed.


Execution of DML statements in PL/SQL block

SQL> create table dept1(dept_id int,dept_name char(25));

Table created.

SQL> begin
insert into dept1 values(1,'sales');
end;
/

PL/SQL procedure successfully completed.

SQL> begin
update dept1 set dept_name='finance' where dept_id=1;
end;
/

PL/SQL procedure successfully completed.

SQL> begin
delete from dept1;
end;
/

PL/SQL procedure successfully completed.


Control Structure
IF
SQL> declare
a number(6):=10;
b number(6);
begin
b:=a+12;
if(a<b) then
dbms_output.put_line(a||' is less than '||b);
end if;
end;
/
OUTPUT
10 is less than 22
PL/SQL procedure successfully completed.

If else
SQL>declare
a number(6):=10;
b number(6);
begin
b:=a-2;
if(a<b) then
dbms_output.put_line(a||' is less than '||b);
else
dbms_output.put_line(a||' is greater than '||b);
end if;
end;
/
OUTPUT
10 is greater than 8

PL/SQL procedure successfully completed.


Elseif
SQL> declare
a number;
b number;
c number;
begin
dbms_output.put_line('Enter a: ');
a:=&a;
dbms_output.put_line('Enter b: ');
b:=&b;
dbms_output.put_line('Enter c: ');
c:=&c;
if(a>b) and (a>c) then
dbms_output.put_line(' a is maximum');
elsif(b>a) and (b>c) then
dbms_output.put_line(' b is maximum');
else
dbms_output.put_line(' c is maximum');
end if;
end;
/
OUTPUT
Enter value for a: 12
old 8: a:=&a;
new 8: a:=12;
Enter value for b: 34
old 10: b:=&b;
new 10: b:=34;
Enter value for c: 41
old 12: c:=&c;
new 12: c:=41;
Enter a:
Enter b:
Enter c:
c is maximum

PL/SQL procedure successfully completed.


Case
SQL> declare
gradein CHAR(1):= UPPER('&pgrade');
gcheck VARCHAR2(20);
begin
gcheck:=
CASE gradein
when 'A' then 'Excellent'
when 'B' then 'Very Good'
when 'C' then 'Good'
else 'No such grade'
end;
dbms_output.put_line('Grade :'||gradein ||' Result '||gcheck);
end;
/
OUTPUT
Enter value for pgrade: A
old 2: gradein CHAR(1):= UPPER('&pgrade');
new 2: gradein CHAR(1):= UPPER('A');
Grade :A Result Excellent

PL/SQL procedure successfully completed.


Trigger
SQL> create or replace trigger tr1 before
insert or update or delete on tcl
begin
if to_char(sysdate, 'DY') in ('FRI','SAT','SUN')
then
raise_application_error(-20123,'We cannot perform DML operation on last 3 days of week');
end if;
end;
/

Trigger created.

SQL> insert into tcl values('0863CS211012','Riya');


insert into tcl values('0863CS211012','Riya')
*
ERROR at line 1:
ORA-20123: We cannot perform DML operation on last 3 days of week
ORA-06512: at "SYSTEM.TR1", line 4
ORA-04088: error during execution of trigger 'SYSTEM.TR1'

SQL> create or replace trigger tr2 before


insert or update or delete on tcl
begin
if sysdate=last_day(sysdate)
then
raise_application_error(-20111,'We cannot perform DML operation on last day of month');
end if;
end;
/

Trigger created.
PROCEDURE
SQL>create or replace PROCEDURE greetings
as
begin
dbms_output.put_line('Good Morning Pooja Mam');
end;
/
Procedure created.

SQL> execute greetings;


Good Morning Pooja Mam

PL/SQL procedure successfully completed.

SQL> begin
greetings;
end;
/
Good Morning Pooja Mam

PL/SQL procedure successfully completed.


SQL> create or replace PROCEDURE squareNum(x IN OUT number) IS
begin
x:=x*x;
end;
/
Procedure created.

SQL> declare
a number;
begin
a:=4;
squareNum(a);
dbms_output.put_line('Square of (4): ' ||a);
end;
/
Square of (4): 16
PL/SQL procedure successfully completed.
FUNCTION
SQL> create FUNCTION FindMin( x IN number,y IN number)
return number
is
z number;
begin
if x<y then
z:=x;
else
z:=y;
end if;
return z;
end;
/

Function created.

SQL> declare
a number;
b number;
c number;
begin
a:=5;
b:=34;
c:= FindMin(a,b);
dbms_output.put_line('Maximum of (5,34): '||c);
end;
/
Maximum of (5,34): 5

PL/SQL procedure successfully completed.


Cursor
IMPLICIT CURSOR
SQL>declare
x emp% rowtype;
begin
select * from emp where empno=7369;
dbms_output.put_line(x.empno || ',' || x.ename);
end;
/
EXPLICIT CURSOR

SQL> declare
cursor c1 is select empno,ename,sal from emp;
Vno emp.emp%type;
Vname emp.ename%type;
Vsal emp.sal%type;
begin
open c1;
loop
fetch c1 into Vno,Vname,Vsal;
dbms_output.put_line(Vno || ','||Vname ||','|| Vsal);
end loop;
close c1;
end;
/

You might also like