Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to insert record into table using stored procedure

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: Create a 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: Create 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
as
begin
insert into tblStudent(Name,Gender,TotalMarks) values(@Name,@Gender,@TotalMarks)
end

Step 3: Execute 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


This post first appeared on ASPArticles, please read the originial post: here

Share the post

How to insert record into table using stored procedure

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×