What is SQL Store Procedure

0
2566

SQL Procedure Basic
In this section I have covered following thing about store procedure. Things like:
• What is store procedure
• What are the usages of store procedure
• Creating simple store procedure
• Execution of store procedure
• Creating procedure with parameter
• Alter a procedure
• Deletion of store procedure

Let’s discuss each point in detail about store procedure:

1. What is Store Procedure

Sql procedure is database object that wrap with sql statements. The main idea behind store procedure is that we can write simple/complicate database logic in it, and to execute it as any time we want.

2. Usages of Store Procedure

• It Use to encapsulate our business logic into store procedure. For example instead of writing same business logic again and again in multiple places, it is good to have encapsulated business logic into a procedure and simple call by its name when situation arise.
• It prevent sql injection
• Easy to use with calling application like .NET, PHP, JAVA
• Easy to do modification a business logic/requirement in store procedure

Following is the basic syntax of store procedure


Let’s assume I have following business logic code to execute which populate the top 10 richest person from table tbl_person
Fig 1:
Figure 1.

If I need to execute this query many times in database, instead of writing this code again and again, it is good to put this sql code in procedure.

3. Creating a Basic Store Procedure
I have used simple select statement for creating a store procedure.

• Creation of store procedure always has Create Procedure or Create proc statement
usp_ : Use to declare the naming convention of store-procedure . Usp_ can be used a User store procedure. After naming convention put a sensible name of store-procedure

After creating of procedure hit the execute button so that it store in database object in compile form.

4. Execution of Store Procedure

Store procedures avoid the human effort to writing same set of query again and again. It store in database object in compile form. To execute a store procedure simply calls Execute along with procedure name.

Let look at figure 2. It is displaying top 10 richest people from table tbl_person
Fig 2
figure2
You can see the output is same as we have got output in figure 1 (the business logic query in figure 1).

5. Store Procedure with Parameter

Store procedure provides advantage to pass parameter. Parameter can be used to limit/differ the query result set.
To declare variable in procedure following rules are required.
• Variable name : variable name always begin with @ sign
• Variable data-type : variable must have data type
Let assume we need to find the name of the person in table person_table by passing id as a parameter


Fig 3 : following figure displaying the execution of procedure with paramter
figure3

6. Modifying existing Procedure
Store procedure is easy in modification. To modifying a procedure following syntax is used

To understand in practice, let’s modify our existing Basic Store Procedure usp_Select_top_richest to select only top 5 people from table tbl_person

To test the output of modified procedure let execute usp_Select_top_richest
Figure 4: it now shows the top 5 rich people after modification of a procedure

figure4

7. Deleting a Procedure
Dropping a procedure is pretty much easy in database and has following syntax:


Hence we have learnt the introduction and usage of store-procedure in sql.

LEAVE A REPLY

Please enter your comment!
Please enter your name here