Professional Documents
Culture Documents
Exercise 6
Exercise 6
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:
@c INT,
@result INT OUTPUT
AS
BEGIN
DECLARE @max INT;
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;
RETURN @result;
END
GO
SELECT dbo.fact(10);
Output:
BEGIN
END