Sunday, 25 September 2016

How to insert record into table using Stored Procedure in SQL-Server

In this article, I will explain you how to insert record into table using sql-server stored procedure. We will insert students record into table. Below is the step by step tutorial.

Step 1: Creating a SQL Table

First, we need to create a table. Execute below script to create a new table.

create table tblStudent
Id int primary key identity,
Name varchar(20),
Gender varchar(10),
TotalMarks int 

Step 2: Creating a Stored Procedure

Now, create a stored procedure usp_Insert_Student which will take 3 parameters as input parameters. Below is the script to create procedure.

create procedure usp_Insert_Student  
@Name varchar(20)=null,  
@Gender varchar(10)=null,  
@TotalMarks int  
insert into tblStudent(Name,Gender,TotalMarks) values(@Name,@Gender,@TotalMarks) 

Step 3: Executing the Stored Procedure

Now, the final step is to execute stored procedure. Below is to script to execute stored procedure 4 times which will insert 4 records into table tblStudent.

exec usp_Insert_Student @Name='Rahul',@Gender='Male',@TotalMarks=95
exec usp_Insert_Student @Name='John',@Gender='Male',@TotalMarks=90
exec usp_Insert_Student @Name='Mary',@Gender='Female',@TotalMarks=85
exec usp_Insert_Student @Name='Mathew',@Gender='Male',@TotalMarks=90

Now, now fire the below select query to see the inserted records.

select * from tblStudent

Below is the output.

Id Name Gender TotalMarks
1 Rahul Male 95
2 John Male 90
3 Mary Female 85
4 Mathew Male 90


Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive