0% found this document useful (0 votes)
23 views4 pages

Exercise 6

The document contains examples of PL/SQL stored procedures and functions. It includes procedures to add two numbers, swap two numbers, and find the largest of three numbers. It also includes a function to calculate the factorial of a number and find the sum of digits in a number. The examples demonstrate how to write and call PL/SQL programs.

Uploaded by

RAMALAKSHMI K
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)
23 views4 pages

Exercise 6

The document contains examples of PL/SQL stored procedures and functions. It includes procedures to add two numbers, swap two numbers, and find the largest of three numbers. It also includes a function to calculate the factorial of a number and find the sum of digits in a number. The examples demonstrate how to write and call PL/SQL programs.

Uploaded by

RAMALAKSHMI K
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/ 4

RAMALAKSHMI K

953622104080

EXERCISE 6
Write user defined functions and Stored procedures in SQL
PLSQL procedures:
1.Write a PL/SQL program to perform addition of two numbers.
CREATE PROCEDURE sum1 (@a int, @b int, @c int)
AS
BEGIN
set @c=@a+@b
print 'Addition'
print @c
END
Declare @a int, @b int, @c int
set @a=30
set @b=20
Exec sum1 @a, @b, @c
Output:

2. Write a PL/SQL program to swap two numbers.


CREATE PROCEDURE swap(@a int, @b int)
AS
BEGIN
Print 'Before Swapping'
SET @a = @a + @b
SET @b = @a - @b
SET @a = @a - @b
Print 'After Swapping'
Print @a
Print @b
END

-- Declare variables and set initial values


Declare @a int, @b int
Set @a = 120
Set @b = 80

-- Execute the procedure


Exec swap @a, @b
Output:

3. Write a PL/SQL program to find the largest of three numbers.


CREATE PROCEDURE largest_number
@a INT,
@b INT,
RAMALAKSHMI K
953622104080

@c INT,
@result INT OUTPUT
AS
BEGIN
DECLARE @max INT;

IF @a >= @b AND @a >= @c


BEGIN
SET @max = @a;
END
ELSE IF @b >= @a AND @b >= @c
BEGIN
SET @max = @b;
END
ELSE
BEGIN
SET @max = @c;
END;
SET @result = @max;
END;
GO
DECLARE @num1 INT, @num2 INT, @num3 INT, @max_number INT;
SET @num1 = 5;
SET @num2 = 10;
SET @num3 = 3;
EXEC largest_number @num1, @num2, @num3, @max_number OUTPUT;
Output:

4. Write a PL/SQL code block to calculate the area of a circle for a value of radius varying.
from 3 to 7. Store the radius and the corresponding values of calculated area in an empty.
table named areas, consisting of two columns radius & area.
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_NAME = 'areas')
BEGIN
CREATE TABLE areas (
radius INT,
area FLOAT
)
END
DECLARE @radius INT = 3;
DECLARE @area FLOAT;
WHILE @radius <= 7
BEGIN
SET @area = 3.14159 * @radius * @radius;
INSERT INTO areas (radius, area) VALUES (@radius, @area);
SET @radius = @radius + 1;
END
PRINT 'Area calculation completed and stored in the "areas" table.';
SELECT * FROM areas;
Output:
RAMALAKSHMI K
953622104080

PLSQL functions:
5. Write a PL/SQL program to find the factorial of a given number
CREATE FUNCTION fact(@number int)
RETURNS int
AS
BEGIN
DECLARE @i int = 1;
DECLARE @result int = 1;

WHILE (@i <= @number)


BEGIN
SET @result = @result * @i;
SET @i += 1;
END

RETURN @result;
END
GO

SELECT dbo.fact(10);
Output:

6.Write a PL/SQL program to find the sum of digits in a given number


DECLARE @num INT = 12345;

DECLARE @digit INT;

DECLARE @total INT = 0;

WHILE @num > 0

BEGIN

SET @digit = @num % 10;

SET @total = @total + @digit;

SET @num = @num / 10;


RAMALAKSHMI K
953622104080

END

PRINT 'Sum of digits: ' + CAST(@total AS VARCHAR(10));


Output:

You might also like