You are on page 1of 7

Database and Management System

Lab Report : 8
Stored Procedures
Submitted To:
Sir Shahid Ali
Submitted By:
Muhammad Shoaib Nadeem
16-CP-76
Section :
Omega
Date:
14/3/2019
Department Of Computer Engineering
UET Taxila
Objectives:

The aim of this lab is to understand Procedures functionality.

Stored Procedures

A stored procedure is a prepared SQL code that you can save, so the code can be reused over and
over again. So if you have an SQL query that you write over and over again, save it as a stored
procedure, and then just call it to execute it. You can also pass parameters to a stored procedure,
so that the stored procedure can act based on the parameter value(s) that is passed.

In this lab we have cover

 Creating a simple stored procedure


 Using Input Parameter
 Using Output Parameter
 Deleting a procedure

Creating a simple stored procedure

When creating a stored procedure we can either use CREATE PROCEDURE or CREATE PROC.
After the stored procedure name we need to use the keyword "AS" and then the restis just the
regular SQL code that we would normally execute. One thing to note is that we cannot use the
keyword "GO" within the stored Procedure. Once the SQL Server compiler sees "GO" it assumes
it is the end of the batch. Also, we cannot change database context within the stored procedure
such as using "USEdbName" the reason for this is because this would be a separate batch and a
stored procedure is a collection of only one batch of statements.

CREATE PROCEDURE procedure_name


AS
sql_statement
GO;

EXEC procedure_name

Using Parameters

Parameters are used to exchange data between stored procedures and functions and the
application or tool that called the stored procedure or function:

 Input parameters allow the caller to pass a data value to the stored procedure or function.
 Output parameters allow the stored procedure to pass a data value or a cursor variable back
to the caller. User-defined functions cannot specify output parameters.
 Every stored procedure returns an integer return code to the caller.

Syntax:
USE Database_name
GO
CREATE PROCEDURE Procedure_name @ID Data_type, @NAME Data_type, @AGE Data_type,
@CITY Data_type
AS
INSERT INTO Table_name VALUES(@ID, @NAME, @AGE, @CITY)
GO

EXEC Procedure_name @ID=3,@NAME='Ali',@AGE=20,@CITY='MULTAN';


select * from Table_name;

CREATE PROCEDURE Proc_Persons_GetAddress @City nvarchar(30), @AddressCount


int OUTPUT
AS
SELECT @AddressCount = count(*)
FROM dbmsLab.Person
WHERE City = @City

We could change this stored procedure and use the ISNULL function to get around this. So if a
value is passed it will use the value to narrow the result set and if a value is not passed it will
return all records.

ISNULL(expression, alt_value)
USE dbmsLab
GO
CREATE PROCEDURE Proc_Persons_GetAddress @City nvarchar(30) = NULL
AS
SELECT * FROM Person
WHERE City = ISNULL(@City,City) -- try as ISNULL(@City, ‘ali’)
GO

ALTER PROCEDURE
Modifying or ALTERing a stored procedure is pretty simple. Once a stored procedure has
been created it is stored within one of the system tables in the database where it created
in. When you modify a stored procedure the entry that was originally made in the system
table is replaced by this new code. Also, SQL Server will recompile the stored procedure
the next time it is run, so your users are using the new logic. The command to modify an
existing stored procedure is ALTER PROCEDURE or ALTER PROC.

ALTER PROCEDURE Proc_Persons_GetAddress @City nvarchar(30)


AS
SELECT * FROM Person
WHERE City LIKE @City + '%'
GO

DROP PROCUDEURE
The syntax is very straightforward to drop a stored procedure.
Dropping Single Stored Procedure
To drop a single stored procedure you use the DROP PROCEDURE or DROP PROC
command as follows.
DROP PROCEDURE Proc_Persons_GetAddress
GO
-- or
DROP PROC Proc_Persons_GetAddress
GO
Dropping Multiple Stored Procedures
To drop multiple stored procedures with one command you specify each procedure
separated
by a comma as shown below.
DROP PROCEDURE Proc_Persons_GetAddress, Proc_Persons_InsertAddress,
Proc_Persons_DeleteAddress
GO
TASK 1:

Create a table named as Persons.


Now create a procedure to insert following values into this table. The values will be given by
passing parameters to the procedure.

TASK 2:
CREATE two tables
Create a Procedure that “retrieves those employees whose department is Computer
Engineering and also shows the name of head of that department”. Use Simple Join in the
Query.
Summary
In this lab, we learn about Stored Procedures. Now, we are able to create a procedure and we
can call procedure by parameters both input and output. We also can alter or drop a parameter.
In addition to running the same SQL code over and over again we also have the ability to pass
parameters to the stored procedure, so depending on what the need is, stored procedure can
act accordingly based on the parameter values that were passed.

You might also like