Advanced SQL - LAB 3
Advanced SQL - LAB 3
Subqueries,
DCL and TCL
Statements
Variables,
Selection, and
Iteration
Stored
Procedures
Tables and
Functions
Error
Handling
Using DDL Statements The Knowledge Academy
Page | 1
Using DDL Statements The Knowledge Academy
1. Overview
The Lab 3 of Advanced SQL course deals with Stored Procedures of different types,
Parameters, and Return values.
Tasks
Stored Procedures
Passing Parameters
Returning Values
System Stored Procedures
Cursors
Triggers
Page | 2
Using DDL Statements The Knowledge Academy
3. Stored Procedures
Stored Procedures contain valid SQL Server statement which are stored as a group
under a name. These procedures can be called whenever required. Stored Procedures
are present under the PROGRAMIBILTY node in the current database.
3.1 Creating a Stored Procedure
A stored procedure can be created in two ways – using the Object Explorer or using
the Query Window.
3.1.1 Using the Object Explorer
To create a Stored Procedure using the Object Explorer follow the steps
Locate the Database CustomerOrders
Expand it to show the nodes under it
Locate the Programmability node and expand it
The first node under Programmability is the Stored Procedure node as shown
Right Click the Stored Procedures node to bring up the context menu as shown
Page | 3
Using DDL Statements The Knowledge Academy
This brings up a template code where the values such as Procedure Name and
Parameters can be substituted as required
Replace the code you want to execute by using this procedure
Click EXECUTE button from the Standard Toolbar
Click Refresh from the Stored Procedures Context Menu
You can find the procedure you created
3.1.2 Using the Query Window
It is much easier to create a stored procedure from the Query Window. Follow the steps
to create the procedure CustRecs that displays all the records from the Customer table
who reside in a specific city.
Click New Query option from the Standard Toolbar
Type the following code in the Query Window
Create Procedure AllCust
as
Begin
Select * from Customer order by Country, City
End
Click Execute from the Standard Toolbar
Type the following code in the Query Window
Exec AllCust
Click Execute from the Standard Toolbar
Page | 4
Using DDL Statements The Knowledge Academy
There are different ways of executing a stored procedure. They are using the object
explorer, using the query window, or calling from another application.
3.2.1 Using the Object Explorer
Page | 5
Using DDL Statements The Knowledge Academy
Click OK
You see the following result
Page | 6
Using DDL Statements The Knowledge Academy
Exec AllCust
Click Execute from the Standard Toolbar
You get the output as follows
The procedures created in SQL Server can also be used by applications that use the SQL
SERVER as their back-end database. As an example, VB.Net calls the stored procedure
using the CALL statement as follows
CALL AllCust
This code can be executed from within the application and not from anywhere in the SQL
Server database.
3.3 Parameters
Parameters are extra information that are provided to the procedures for completing its
execution. A procedure may or may not have any parameters. The following example list
all customers living in Sydney.
3.3.1 Creating Parameter based Procedure
Create Procedure CustRecs
@City varchar (30)
as
Begin
Select * from Customers Where City = @City
End
Page | 7
Using DDL Statements The Knowledge Academy
Page | 8
Using DDL Statements The Knowledge Academy
Page | 9
Using DDL Statements The Knowledge Academy
Page | 10
Using DDL Statements The Knowledge Academy
Page | 11
Using DDL Statements The Knowledge Academy
Page | 12
Using DDL Statements The Knowledge Academy
Page | 13
Using DDL Statements The Knowledge Academy
5. Cursors
Cursors in SQL Server are used as a means to retrieve multiple rows one by one. There
are various types of cursors such as Forward Only, and Scroll Cursors.
5.1 Forward Only Cursors
Forward only cursors have the ability of moving from the beginning towards the end of
the result set. They cannot jump to a random position or even move backward.
Type the following in the Query Window
Declare @CustName varchar (30)
Declare @City varchar (30)
Declare CustCur Cursor
for Select CustomerName, City from Customer
Begin
Open CustCur
Fetch CustCur into @CustName, @City
Print 'Customer Name:'+@CustName
Print ‘City:'+@City
Close CustCur
End
Deallocate CustCur
Click the Execute option from the Standard Toolbar
You get the following output
Page | 14
Using DDL Statements The Knowledge Academy
Page | 15
Using DDL Statements The Knowledge Academy
Page | 16
Using DDL Statements The Knowledge Academy
6. Triggers
Triggers in SQL Server are procedures which execute whenever an action takes place.
They are generally classified into simple triggers and INSTEADOF triggers
6.1 Simple Triggers
Create a Trigger to disallow any DDL operations on the CustomerOrders database.
Type the following in the Query Window
USE [CustomerOrders]
GO
Create Trigger [Trig] On Database
For Create_Table, ALter_Table, Drop_Table
As
Print'DDL Operations Not Allowed On This Database'
Rollback Tran
Page | 17
Using DDL Statements The Knowledge Academy
Page | 18
Using DDL Statements The Knowledge Academy
INSTEADOF Triggers are those triggers whose statement will be executed INSTEAD OF
the DML Statement to which the trigger is linked. If an INSTEADOF Trigger exists for an
INSERT in table Customer, the statements in the trigger will execute instead of the
INSERT statement.
Synchronize the two tables Customer and CustomerBak to contain the latest INSERTED
records
Type the following in the Query Window
Use CustomerOrders
Go
Create Trigger InstTrig
On Customer
INSTEAD of INSERT
AS
Begin
INSERT INTO CUSTOMER
Select I.CustomerId, I.CustomerName, I.City,
I.Country, I.PostalCode From Inserted I
INSERT INTO CustomerBackup
Select I.CustomerId, I.CustomerName, I.City,
I.Country, I.PostalCode From Inserted I
END
Select * from CustomerBackup
Page | 19
Using DDL Statements The Knowledge Academy
Page | 20