Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

PLSQL ALL Program

hope it is helpfull

Uploaded by

Siddhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PLSQL ALL Program

hope it is helpfull

Uploaded by

Siddhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Important PL SQL Programs

1) find factorial number using for loop


declare
fact number:=1;
n number := 6;
begin
for i in 1..n loop
fact:=n*fact;
n:=n-1;
end loop;
dbms_output.put_line('factorial number is'||fact);
end;
2) Display Even number from given number range.
declare
x number := 0;
begin
for x in 1..100 loop
if mod(x,2)=0 then
dbms_output.put_line ( x);
end if;
end loop;
end;

3) Write a PLSQL program to print reverse numbers from 1 to


10 using for loop.
declare
n number := 10;
begin
dbms_output.put_line ('output is reversed');
for n in reverse 1..10 loop
dbms_output.put_line (n);
end loop;
end;
Important PL SQL Programs
4) Write a PLSQL program to print numbers from 1 to 10 using
for loop.
declare
n number;
begin
for n in 1..10 loop
dbms_output.put_line (n);
end loop;
end;

5) Display Even or odd number from given number.


declare
n number:=2;
r number;
begin
r := mod(n, 2);
if r = 0 then
dbms_output.put_line('even');
else
dbms_output.put_line('odd');
end if;
end;

6) Find factorial Number using while loop


declare
fact number :=1;
n number := 4;
begin
while n > 0 loop
fact:=n*fact;
n:=n-1;
end loop;
dbms_output.put_line('fact'||fact);
end;
Important PL SQL Programs
7) Find the area of the Rectangle
declare
Length number;
Breadth number;
Area number;
begin
Length:=5;
Breadth:=10;
Area:= Length * Breadth ;
dbms_output.put_line('Area of the rectangle'||Area);
end;

8) Find the less and greater number.


declare
n1 int;
n2 int;
begin
n1:=10;
n2:=20;
if n1 < n2 then
dbms_output.put_line(n1 || 'is less then:='||n2);
end if;
end;

9) Find the area of the circle.


declare
p constant number:=3.14;
r number(10);
a number(3);
begin
r:=5;
a:=p*r*r;
dbms_output.put_line('area of a:'||a);
end;
Important PL SQL Programs
10) Write a code using if-else condition
declare
n1 int;
n2 int;
begin
n1:=5;
n2:=10;
if n1 > n2 then
dbms_output.put_line('n2 is greater then:='||n1);
elsif n1 < n2 then
dbms_output.put_line('n1 is less then:='||n2);
end if;
end;
11) Write a PL/SQL code to print largest number from three
numbers (accept three numbers from user).

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 GREATEST'||A);
elsif (b>a) and (b>c) then
dbms_output.put_line('B is GREATEST'||B);
else
dbms_output.put_line('C is GREATEST'||C);
end if;
End;
Important PL SQL Programs
Explicit Cursor

DECLARE
id customer.id%type;
name customer.name%type;
salary customer.salary%type;
CURSOR c_customer is
SELECT id, name, salary FROM customer;
BEGIN
OPEN c_customer;
LOOP
FETCH c_customer into id, name, salary;
EXIT WHEN c_customer%notfound;
dbms_output.put_line(id || ' ' || name || ' ' || salary);
END LOOP;
CLOSE c_customer;
END;

Implicit Cursor

create table customer(id int, name varchar(20),salary number);


insert into customer values(1,'ABC',5000;
select * from customer;
Program
DECLARE
total_rows number(2);
BEGIN
UPDATE customer SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customer selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customer selected ');
END IF;
END;
Important PL SQL Programs
 Procedure
Created Procedure
create or replace procedure v1
AS
BEGIN
dbms_output.put_line('Hello World!');
END;

 execute v1;

 drop procedure v1;

Parameter Modes in PL/SQL Subprograms


1) DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 2;
squareNum(a);
dbms_output.put_line(' Square of (2): ' || a);
END;
/
Important PL SQL Programs
2) DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT
number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 2;
b:= 5;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (2, 5) : ' || c);
END;
Function
create table customer(id int, name varchar(20), salary number);
insert into customer values(1,'ABC',3000);
select * from customer;
CREATE OR REPLACE FUNCTION totalCustomer
RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customer;

RETURN total;
END;
/
Important PL SQL Programs
Declaring Function:

DECLARE
c number(5);
BEGIN
c := totalCustomer();
dbms_output.put_line('Total no. of Customer: ' || c);
END;

Trigger
1) Create table main(id int, salary int);
2) Create table backup(id int, salary int);
Create trigger
Create or replace trigger t1
before delete on main
for each row
begin
insert into backup values(:old.id, :old.salary);
end;
Types of Triggers
1)DDL Triggers
2)DML Triggers
3)Logon Triggers

You might also like