DBMS Ex9
DBMS Ex9
Aim:
To execute the functions, procedures and Packages in SQL
Description
Functions:
A function is a named PL/SQL Block which is similar to a procedure. The major
difference
between a procedure and a function is, a function must always return a value, but a
procedure
may or may not return a value.
SYNTAX
Application :
To calculate the shipping cost based on the provided weight and distance values.
total_cost NUMBER;
BEGIN
total_cost := (weight_in_kg * base_rate_per_kg) + (distance_in_km *
rate_per_km);
RETURN total_cost;
END calculate_shipping_cost;
/
M BARATH RAJ-22IT012
RESULT :
Procedures :
A procedure is a group of PL/SQL statements that you can call by name.
A procedure is a module performing one or more actions , it does not
need to return any values.
Creating a Procedure:
Syntax
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END
procedure_name;
APPLICATION ;
TO UPDATE SAVNIGS :
v_total_rows := SQL%ROWCOUNT;
IF v_total_rows = 0 THEN
dbms_output.put_line('No records updated.');
ELSE
dbms_output.put_line(v_total_rows || ' records updated.');
END IF;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('An error occurred: ' || SQLERRM);
END update_savings;
/
OUTPUT :
M BARATH RAJ-22IT012
PACKAGES:
Packages are schema objects that groups logically related PL/SQL types,
variables, and subprograms.
A package will have two mandatory parts −
Package specification
Package body or definition .
SYNTAX:
For creating package:
CREATE [OR REPLACE] PACKAGE <package_name>
IS
<sub_program and public element declaration>
.
.
END <package name>
END <package_name>
M BARATH RAJ-22IT012
APPLICATION :
M BARATH RAJ-22IT012
Result:
Thus the functions , procedures and packages are successfully executed in SQL.