Skip to main content

Stored Procedure Explained


SQL Stored Procedure Explained


A SQL Server stored procedure, groups one or more Transact-SQL statements into a logical unit and is stored as an object in the Database Server. When a stored procedure is called at the first time, SQL Server creates an execution plan and stores it in the plan cache. In the subsequent executions of the stored procedure, SQL Server reuses the plan so that the stored procedure can execute very fast with reliable performance as explained in SQL Server Tutorial site


This post introduces you to the stored procedures and shows you how to develop flexible stored procedures to optimize your database access whether with C# or SQL Server Management Studio.

Connect to SQL Server through Microsoft SQL Server Management Studio as shown below




Stored Procedure Syntax (Create)


CREATE PROCEDURE procedure_name
AS
BEGIN
sql_statement
END

EXC procedure_name

Explanation of the syntax


CREATE is a keyword in Transact sql for creating new stored procedure.
PROCEDURE is also a keyword in Transact SQL that signifies creating a stored procedure. We can also use PROC keyword instead of the long name.
AS keyword is to specify the transact sql statements or command logics.
BEGIN and END is use to wrap the t-sql statements if we have more than one, but can be omitted if it’s only one statement.

We are going to be using the csharpnaija_employees database and employees, departments tables created from previous post.

Employees table




Departments table




 In this post, we will learn how to manage stored procedures in SQL Server including creating, executing, modifying, and deleting stored procedures.

Creating a Stored Procedure

For instance, if we want to get records from employees table, we use the below code
SELECT * FROM employees                      
But if we are going to be using this statement in several places, we create a stored procedure that encapsulate the above statement so as to reuse the code.
To create a stored procedure that wraps this query, you use the CREATE PROCEDURE statement as follows
We can graphically create this stored procedure in MSSMS as outlined below
Expand csharpnaija_employees database from object explorer as below



Expand the programmability folder under the csharpnaija_employees as shown below

Right click on Stored Procedures folder, Select New and click on Stored Procedure as shown below

From the new stored procedure template automatically created, change the procedure name, parameters and the statement as shown below

Change the template to look like the below
-- =============================================

-- Author:           <Musa Sule>
-- Create date: <20/01/2020>
-- Description:      <Get All Employees with their respective departments>
-- =============================================
CREATE PROCEDURE GetAllEmployees
AS
BEGIN
       SET NOCOUNT ON;
       SELECT e.FirrstName,e.MiddleName,e.LastName,e.PhoneNumber,e.Address, d.DeptName
       FROM employees e
       INNER JOIN departments d
       ON e.DepartmentId=d.Id
END
GO

Click on Execute  to create and save the procedure for later use
If everything is ok, Commands completed successfully as shown in the Messages window below
Now expand the stored procedures folder to see the newly created procedure as shown below

To execute the created procedure, we call EXECUTE sp_name;

EXEC GetAllEmployees


Modifying a stored procedure

To modify an existing stored procedure, you use the ALTER PROCEDURE statement.
First, open the stored procedures to view its contents by right-clicking the stored procedure name you want to modify and select Modify as shown below

The stored procedure to be modified will open in query window as shown below


Change anything you want to change from the T-SQL statements and click on Execute as shown below


The result of the modified stored procedure is as shown below



Deleting a stored procedure


To delete a stored procedure, we will use the DROP PROCEDURE or DROP PROC statement in the query window.

Syntax


DROP PROCEDURE sp_name; or DROP PROC sp_name;

We can also use the Graphical User Interface in the Microsoft SQL Server Management Studio
Right click on the stored procedure you want to delete as shown below   

A Delete Object window will be displayed, click on OK to Delete the stored Procedure as shown below

The stored procedure will deleted and removed from the Stored Procedure folder.

Comments

Popular posts from this blog

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

Most Popular Programming Languages in 2020

Most Popular Programming Languages in 2020 In this blog post, you will learn about the most popular programming languages in 2020 for creating the best web applications. Check its pros and cons. Analyzed by technostacks Not very long ago, just a few people were considered to be computer programmers, and the general public viewed them with awe. In this digital age that we are now living in, however, a large number of IT jobs need a solid grasp of one or more programming languages. Whether one wants to develop a mobile app or get a certification for having programming knowledge, or even to learn new skills, one needs to opt for the right programming language. Below mentioned eight most popular programming languages which are in demand for software development and web applications. This is the most used programming languages in 2019 and will be in 2020. For each, there is little information about the language, benefits and its complexity, as well as about its usage. One must...

HashTable in C# with Example

  HashTable in C# with Example Hashtable  is used to store a collection of key/value pairs of different  data types  and are organized based on the hash code of the key.   Generally, the hashtable object will contain buckets to store elements of the collection. The bucket here, is a virtual subgroup of elements within the hashtable and each bucket is associated with a hash code, which is generated based on the key of an element.   In C#, hashtable is same as a  dictionary  object but the only difference is that the  dictionary  object is used to store a key-value pair of same  data type  elements.   When compared with  dictionary  object, the hashtable will provide a lower performance because the hashtable elements are of object type so the boxing and unboxing process will occur when we are storing or retrieving values from the hashtable.   C# HashTable Declaration Hashtable is a non-generic type...