DBDM Ex - No.6

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

EX.

NO:6 STORED PROCEDURES / FUCTIONS

AIM

To write a PL/SQL block to create and display procedures

ALGORITHM

STEP 1:Start querying after establishing connection in Run SQL Command Line console
window.

STEP 2: Create the producer with Producer_Name.

STEP 3: Declare the necessary variables by using DECLARE and initialize the variable
by Begin keyword

STEP 4: If the exceptions is thrown, check the syntax and logics.


STEP 5: If the query is running successfully, stop querying
QUERIES
SAMPLE PROCEDURE

SQL> SET SERVEROUTPUT ON

SQL> CREATE OR REPLACE PROCEDURE PROC1 AS

BEGIN

DBMS_OUTPUT.PUT_LINE('Hello from PRODCURE...');

END;

/
Output

PROCEDURE FOR SUMMING TWO NUMBERS

SQL> CREATE OR REPLACE PROCEDURE PROC2


(N1 IN NUMBER,N2 IN NUMBER,TOT OUT NUMBER) IS
BEGIN
TOT := N1 + N2;
END;

Output
PRINTING THE VARIABLE DURING EXECUTION

SQL> VARIABLE T NUMBER

SQL> EXEC PROC2(33,66,:T)

Output

PROCEDURE FOR GCD NUMBERS


create or replace procedure pro
is
a number(3);
b number(3);
c number(3);
d number(3);
begin
a:=&a;
b:=&b;
if(a>b) then
c:=mod(a,b);
if(c=0) then
dbms_output.put_line('GCD is');
dbms_output.put_line(b);
else
dbms_output.put_line('GCD is');
dbms_output.put_line(c);
end if;
d:=mod(b,a);
else
if(d=0) then
dbms_output.put_line('GCD is');
dbms_output.put_line(a);
else
dbms_output.put_line('GCD is');
dbms_output.put_line(d);
end if;
end if;
end;
/

Output

FUNCTION FOR FACTORIAL

Create or replace function fact (n number) return number


as
fac number:=1;
Begin
for i in 1..n
Loop
fac:=fac*i;
end loop;
returnfac;
end;
/
DATABASE MANAGEMENT
SYSTEMS LABORATORY
Output

DISPLAYING FUNCTION

Select fact(4) from dual;

Output

PROGRAM FOR CUBE FUNCTION

Create or replace function cub (n number) return number


as
c number;
begin
c:=n*n*n;
return c;
end;
/

6
DATABASE MANAGEMENT
SYSTEMS LABORATORY
Output

DISPLAYING FUNCTION

Select cub(2) from dual;

Output

RESULT

Thus the Procedures using Pl/SQL was created and executed

7
DATABASE MANAGEMENT
SYSTEMS LABORATORY
successfully.
Output

You might also like