Stored Procedure in SQL Server 2012
In this article I described Stored Procedures in SQL Server 2012.
In this article I give a short description of Stored Procedures. In this article I describe how to create a Stored Procedure, use of Stored Procedures, the benefits of Stored Procedures and Stored Procedures for insert, update, delete and join.
Store Procedures are a precompiled set of one or more statements that are stored together in the database. They reduce the network load because of the precompilation. We can create a Stored Procedure using the "Create proc" statement.
Why we use Stored Procedure
There are several reasons to use a Stored Procedure. They are a network load reducer and decrease execution time because they are precompiled. The most important use of a Stored Procedure is for security purposes. They can restrict SQL Injection. We can avoid SQL injection by use of a Stored Procedure.
Difference Between Stored Procedure and Function:
- A Stored Procedure may or may not return a value but a function always returns a value.
- We can use a function in a Stored Procedure but we can't use a Stored Procedure in a Function
Here we create two tables named emp and emp1 on which we apply a Stored Procedure.
Creation of emp table:
create table emp(empId int, empName varchar(15), empAge int )
insert into emp
select 1,'deepak',21 union all
select 2,'Arora',21 union all
select * from emp
Creation of emp1 table
create table emp1(empId int,empAdd varchar(35))
Inserting the value
insert into emp1
select 1,'delhi'union all
select 2,'banglore'union all
select * from emp1
Type of Stored Procedure
System Stored Procedure
User Defined Stored Procedure
Here I describe only User Defined Stored Procedure
Types of User Defined Store Procedure:
Non-parameterized Stored Procedure
Parameterized Stored Procedure
Non-parameterized Stored Procedure:
This type of Stored Procedure does not accept parameters.
Non-parameterized Stored Procedure for Showing All Records
create proc usp_select
select * from emp
Non-parameterized Stored Procedure for Join:
create proc usp_join
select e.empId,e.empName,e.empAge,e1.empAdd from emp e inner join emp1 e1 on e.empId=e1.empId
Parameterized Stored Procedure:
These accept one or more Parameters. It is important that the data type of the parameters be the same as the data type in the tables.
Parameterized Stored Procedure for Insert
create proc usp_insert(@id int,@name varchar(15),@age int)
insert into emp values(@id,@name,@age)
exec usp_insert 4,'Ankit',19
Parameterized Stored Procedure for Update
create proc usp_update(@id int,@name varchar(15),@age int)
update emp set empName=@name,empAge=@age where empId=@id
exec usp_update 4,'Mohan',20
Parameterized Store Procedure for delete
create proc usp_delete(@id int)
delete from emp where empId=@id
exec usp_delete 4
In this article I described Store Procedures in SQL Server. I hope this article has helped you to understand this topic. Please share if you know more about this. Your feedback and constructive contributions are welcome.