SQL Views & Procedures
SQL Views & Procedures
SQL Views & Procedures
1
Views
Output of a relational query such as SELECT is another
relation (or table).
If we have some query that needs to be executed
frequently, then it’s a good idea to have that query stored
as a view.
A view is a virtual table based on a SELECT query. The
query can contain columns, computed columns, aliases,
and aggregate functions from one or more tables.
The tables on which the view is based are called base
tables.
2
Views
A view can be created by using CREATE VIEW command:
CREATE VIEW viewname AS SELECT query
4
Procedural Extensions
If we want to perform a conditional or looping type of
operation (i.e., a procedural type of programming using an IF-
THEN-ELSE or DO-WHILE statement) is required!
To remedy the lack of procedural functionality in SQL, the
SQL-99 standard defined the use of persistent stored
modules (PSM) in the database software!
A PSM is a block of code containing standard SQL statements
and procedural extensions that is stored and executed at the
DBMS server.
PSM represents business logic that can be encapsulated,
stored, and shared among multiple database users.
5
Procedural Extensions
Oracle implements PSMs through its procedural SQL
language (PL/SQL).
6
Stored Procedures
A stored procedure is a named collection of procedural and
SQL statements.
Stored procedures encapsulate and represent business
transactions.
Syntax:
CREATE OR REPLACE PROCEDURE procedure_name
[(argument
[IN/OUT] data-type, … )]
[IS/AS]
BEGIN
[ variable_namedata type[:=initial_value] ]
SQL statements;
…
END
GO 7
Example: Stored Procedures
An example of creating a stored procedure to get details of
vendors:
https://www.w3schools.com/sql/sql_stored_procedures.asp 8
Stored Procedures (ctd.)
Stored procedures can be parametrized as well, i.e. we can
pass parameters to the stored procedures, an example:
https://learn.microsoft.com/en-us/sql/t-sql/lesson-1-creating-database-objects? 9
Exercises
Create these stored procedures:
or,
12
Stored Procedures with IN/OUT
parameters (ctd.)
We can execute this stored procedure as:
CREATE PROCEDURE getProductCount (
@product_count INT OUTPUT
) AS
BEGIN
SELECT * from product;
13
Procedural Code Example
The following procedural code computes area and perimeter of a
square as:
DECLARE @v_side INT, @v_perimeter INT, @v_area INT;
BEGIN
SET @v_side = 5;
14
Procedural Code Example (ctd.)
The following procedural code demonstrated the use of IF-ELSE:
DECLARE @v_number INT;
BEGIN
SET @v_number = -10;
IF @v_number = 0
PRINT('Zero entered');
ELSE
BEGIN
IF @v_number > 0
PRINT('Positive’);
ELSE
PRINT('Negative’);
END;
END;
15
IF/ELSE will only count the line right below.
EX
IF a > 200
Print(‘a is good’)
Print(‘Goodbye’)
The Goodbye statement is outside of the If statement and will execute
regardless.
TO add multiple lines in an if statement, preface them with begin and
finish them with end.
16
Procedural Code Example (ctd.)
The following procedural code demonstrated the use of WHILE:
DECLARE @v_number INT;
BEGIN
SET @v_number = 5;
17
Exercises
Create the stored procedures:
That accepts customer code and finds the count of orders placed
by that customer. If the count of orders placed and display the
message: Custmer <code> ‘placed one order!’ if the count is 1
else display: Custmer <code> ‘placed more than one order!’.
18
Error Handling
An example of error handling:
BEGIN TRY
select 1/0
--delete from vendor where v_code = 21231
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_MESSAGE() AS ErrorMessage;
END CATCH
MySQL Reference:
https://dev.mysql.com/doc/refman/8.0/en/condition-handling.html
https://www.mysqltutorial.org/mysql-error-handling-in-stored-procedures/
19
Cursor
A cursor is a special construct used in procedural SQL to hold
the data rows returned by a SQL query.
Cursor can be considered as a reserved area of memory in
which the output of the query is stored, like an array holding
columns and rows.
Cursors are held in a reserved memory area in the DBMS
server, not in the client computer!
OPEN vendor_cursor
FETCH NEXT FROM vendor_cursor INTO @code, @name, @state
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor; 21
Benefits of Stored Procedures
Improved Performance: Stored procedures are pre-compiled and
stored on the server, so they can be executed more quickly than
SQL statements that are sent from client applications.
Code Reuse: Stored procedures can be called from different
client applications, i.e. the same code can be reused across
different applications. This reduces development time and
maintenance costs.
Reduced Network Traffic: Since stored procedures are executed
on the server, only the results are returned to the client, therefore
network traffic gets reduced improving application performance.
Better Security: Stored procedures can be used to enforce
security rules and prevent unauthorized access to sensitive data.
22
Drawbacks of Stored Procedures
Increased Overhead: Stored procedures can consume more
server resources than simple SQL statements, particularly when
they are used frequently.
Limited Portability: Stored procedures are often specific to a
particular DBMS, therefore may not be easily portable to other
databases.
Debugging Challenges: Debugging stored procedures can be
more challenging than debugging simple SQL statements.
Security Risks: If not written correctly, stored procedures can
pose a security risk, if they are used to access sensitive data or
to perform actions that could compromise the integrity of the
database.
23