LAB 08 (Procedure, Functions, Views)
LAB 08 (Procedure, Functions, Views)
Characteristics of Procedure:
Parameter:
The parameter is variable or placeholder of any valid PL/SQL datatype through which
the PL/SQL subprogram exchange the values with the main code. This parameter
allows to give input to the subprograms and to extract from these subprograms.
These parameters should be defined along with the subprograms at the time of
creation.
These parameters are included n the calling statement of these subprograms to
interact the values with the subprograms.
The datatype of the parameter in the subprogram and the calling statement
should be same.
The size of the datatype should not mention at the time of parameter
declaration, as the size is dynamic for this type.
Based on their purpose parameters are classified as
1. IN Parameter
2. OUT Parameter
3. IN OUT Parameter
IN Parameter:
This parameter is used for giving input to the subprograms.
It is a read-only variable inside the subprograms. Their values cannot be
changed inside the subprogram.
In the calling statement, these parameters can be a variable or a literal value or
an expression, for example, it could be the arithmetic expression like '5*8' or
'a/b' where 'a' and 'b' are variables.
By default, the parameters are of IN type.
OUT Parameter:
This parameter is used for getting output from the subprograms.
It is a read-write variable inside the subprograms. Their values can be changed
inside the subprograms.
In the calling statement, these parameters should always be a variable to hold
the value from the current subprograms.
IN OUT Parameter:
This parameter is used for both giving input and for getting output from the
subprograms.
It is a read-write variable inside the subprograms. Their values can be changed
inside the subprograms.
In the calling statement, these parameters should always be a variable to hold
the value from the subprograms.
Syntax:
CREATE OR REPLACE PROCEDURE
<procedure_name>
(
<parameterl IN/OUT <datatype>
..
.
)
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
EXCEPTION
<exception handling part>
END;
In this example, we are going to create a procedure that takes the name as input
and prints the welcome message as output. We are going to use EXEC command
to call procedure.
Function?
Functions is a standalone PL/SQL subprogram. Like PL/SQL procedure, functions
have a unique name by which it can be referred. These are stored as PL/SQL database
objects. Below are some of the characteristics of functions.
Functions are a standalone block that is mainly used for calculation purpose.
Function use RETURN keyword to return the value, and the datatype of this is
defined at the time of creation.
A Function should either return a value or raise the exception, i.e. return is
mandatory in functions.
Function with no DML statements can be directly called in SELECT query
whereas the function with DML operation can only be called from other
PL/SQL blocks.
It can have nested blocks, or it can be defined and nested inside the other
blocks or packages.
It contains declaration part (optional), execution part, exception handling part
(optional).
The values can be passed into the function or fetched from the procedure
through the parameters.
These parameters should be included in the calling statement.
Function can also return the value through OUT parameters other than using
RETURN.
Since it will always return the value, in calling statement it always accompanies
with assignment operator to populate the variables.
Syntax
CREATE OR REPLACE FUNCTION
<procedure_name>
(
<parameterl IN/OUT <datatype>
)
RETURN <datatype>
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
EXCEPTION
<exception handling part>
END;
IS
datevalue DATE;
BEGIN
SELECT SYSDATE
INTO datevalue
FROM dual;
END;
SQL view:
SQL view is nothing but a logical table or a virtual table stored in a database. We can also define a
VIEW as SELECT Statement with a name which is stored in a database as though it were a table. All
the DML commands which you can perform on a table can be performed on a view also.
1. Security
2. Reducing complexity of a query.
Security:
Using a VIEW you can mask columns of a table and restrict any user to use the data from that
column. For example,
Suppose you have a large table containing a mixture of both sensitive as well as general interest
information. In this case it will be very handy for you to create a view which only queries the general
interest columns of the original table and in turn grants privileges on this view to the general users. In
this case the population of general users can query the view and have direct access only to the
general information omitting the sensitive information present in the underlying table. Thus in this
way views can be used to give access to selective data in a table.
A view built on a complex join can be created in order to include the complexity in the view itself.
The result of this is a regular view object that looks to be a single table which could be queried by
you as any regular table. The view can be joined with other views and tables as well. Thus here in
this way, view can be used to reduce the complexity of a common Join.
Syntax
The syntax for the CREATE OR REPLACE VIEW Statement in Oracle/PLSQL is: