Introduction
Here, we will see how to create select, insert, update, and delete statements using stored procedures in SQL Server. Let's take a look at a practical example. We create a table.
Create a Table in SQL
CREATE TABLE employee
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR(10),
last_name VARCHAR(10),
salary DECIMAL(10, 2),
city VARCHAR(20),
)Now add some rows to the table. We can add new rows using an INSERT INTO SQL statement. Then execute a SELECT SQL query to display all records of the table.
INSERT INTO employee
VALUES (2,
'Monu',
'Rathor',
4789,
'Agra');
go
INSERT INTO employee
VALUES (4,
'Rahul',
'Saxena',
5567,
'London');
go
INSERT INTO employee
VALUES (5,
'prabhat',
'kumar',
4467,
'Bombay');
go
INSERT INTO employee
VALUES (6,
'ramu',
'kksingh',
3456,
'jk');
go
SELECT *
FROM employeeThe table looks like this.

Stored Procedure for Select, Insert, Update, Delete
Here, we create a stored procedure with SELECT, INSERT, UPDATE, and DELETE SQL statements. The SELECT SQL statement is used to fetch rows from a database table. The INSERT statement is used to add new rows to a table. The UPDATE statement is used to edit and update the values of an existing record. The DELETE statement is used to delete records from a database table. The following SQL stored procedure is used insert, update, delete, and select rows from a table, depending on the statement type parameter.
ALTER PROCEDURE Masterinsertupdatedelete (@id INTEGER,
@first_name VARCHAR(10),
@last_name VARCHAR(10),
@salary DECIMAL(10, 2),
@city VARCHAR(20),
@StatementType NVARCHAR(20) = '')
AS
BEGIN
IF @StatementType = 'Insert'
BEGIN
INSERT INTO employee
(id,
first_name,
last_name,
salary,
city)
VALUES ( @id,
@first_name,
@last_name,
@salary,
@city)
END
IF @StatementType = 'Select'
BEGIN
SELECT *
FROM employee
END
IF @StatementType = 'Update'
BEGIN
UPDATE employee
SET first_name = @first_name,
last_name = @last_name,
salary = @salary,
city = @city
WHERE id = @id
END
ELSE IF @StatementType = 'Delete'
BEGIN
DELETE FROM employee
WHERE id = @id
END
ENDNow press F5 to execute the stored procedure. This will create a new stored procedure in the database.
Now open object explorer and select store procedure MasterInsertUpdateDelete.
Stored Procedure to Check Insert
StatementType = 'Insert'MasterInsertUpdateDelete -> right-click select Execute Stored Procedure.

Execute procedure window will be opened.

Now for insertion, we fill the data in values in the required fields.
StatementType=insert
Click on the OK button.
You will see a new row added to the database table.

Stored Procedure to Check update
MasterInsertUpdateDelete -> right-click select Execute Stored Procedure...
Execute procedure window will be opened.
StatementType = 'Update'
Click on the OK button.
Check the employee table with the following updated data where the id is 7.

Stored Procedure to Check Delete
MasterInsertUpdateDelete -> right-click select Execute Stored Procedure.
Execute procedure window will be opened.
StatementType = 'Delete'
We will delete records from the table which has id=2.
Click on the OK button. And check in the employee table with the following deleted data where the id is 2.

Summary
A single stored procedure can be used to select, add, update, and delete data from a database table. In this article, we learned how to create a single stored procedure to perform all operations using a single SP in SQL Server.


Nurman AdePosted Mar 18, 2022, 2:28 AM EDT
How to call specific stored procedure. For example Exec Masterinsertupdatedelete where StatementType = 'Select'
Máÿàñk GhìldíÿâlPosted Feb 16, 2022, 10:49 AM EST
How to check select query in this stored procedure
roger simanjuntakPosted Dec 19, 2021, 12:29 PM EST
Msg 208, Level 16, State 6, Procedure Masterinsertupdatedelete, Line 2 [Batch Start Line 46]Invalid object name 'Masterinsertupdatedelete'.
Mohamed BassunyPosted Dec 8, 2020, 12:10 AM EST
This code is perfect but only one thing is not as i want , how can i make one Stored Procedure when i don't want to pass "id" in insert only , but i need it for other ? thanks
Mohamed BassunyPosted Dec 7, 2020, 6:10 AM EST
Thanks this is perfect
Umesh DaiyaPosted Apr 9, 2020, 1:21 PM EDT
In this how i can stop duplicate employee name
Chittaranjan SwainPosted Oct 30, 2019, 6:10 AM EDT
Nice article...
Sandipta SahuPosted May 8, 2019, 8:22 PM EDT
How to data-bind to grid view with parameter
Sandipta SahuPosted May 8, 2019, 7:35 PM EDT
How to pass parameters for select comment help me/
Pramod PeyyalaPosted Jan 7, 2019, 8:29 PM EST
Nice Article
Piyush AgarwalPosted Mar 9, 2018, 12:59 AM EST
Thanks for sharing this...
Mohit SharmaPosted Jul 13, 2017, 11:54 PM EDT
If i want to update the stored procedure table column not having any parameters than how it will ??
manish kumarPosted May 19, 2017, 6:50 AM EDT
My query is not working Display Must Declare the Scalar variable
Sa HulPosted Mar 10, 2017, 10:22 PM EST
How to write condition in asp.net for (insert and update, delete) in storedprocedure
kalu singh raoPosted Jul 9, 2016, 6:39 AM EDT
Nice...
Noor MowafaqPosted Dec 6, 2015, 11:31 PM EST
I'm having an error when i execute the procedure saying that the procedure name is invalid and i don't know how to deal with it
nofact nofacePosted May 30, 2015, 11:13 PM EDT
I search internet for 3 hour to find this page thank you
Imran AhmedPosted Jan 30, 2015, 6:51 AM EST
thanks a lot,
Alex MejikPosted Oct 21, 2014, 11:25 PM EDT
when i execute it from SQL server the insert works fine----exec MasterInsertUpdateDelete_student null,'test23','lastname','12/12/2012','London','434343434','Insert'
Alex MejikPosted Oct 21, 2014, 11:24 PM EDT
from c$ i call it - db_contex.MasterInsertUpdateDelete_student(null,fname, lname, bdate, adress, phone,"Insert");
Alex MejikPosted Oct 21, 2014, 11:23 PM EDT
Hey,thanks a lot for the code, but i get an error saying---------- The data reader is incompatible with the specified 'UniDbModel.MasterInsertUpdateDelete_student_Result'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name. -------------- Select works fine, what is the problem?
Vaka SivaramireddyPosted Oct 13, 2014, 9:07 PM EDT
nice sir .simply super
Shreya SoniPosted Aug 30, 2014, 12:15 AM EDT
Its useful.. I need a common stored procedure to update all tables.. A common for all... Plz help me..
Aman PetwalPosted Feb 5, 2014, 5:52 AM EST
its verry helpfull thanx sir ji
Tariq HashmiPosted Jan 29, 2014, 12:57 AM EST
Thanks sir ji
nupur kadamPosted Nov 27, 2013, 2:43 AM EST
thanks
durgesh pandeyPosted Nov 5, 2013, 4:20 AM EST
Thanks!!!!!!!!!!!!! nice 1
Saikat Kumar DeyPosted Aug 7, 2013, 2:30 AM EDT
thanx sir.. i invited u to chat.. my email id is [email protected]
Rohatash KumarTOP 100Posted Aug 7, 2013, 2:28 AM EDT
My Email-id- [email protected]
Saikat Kumar DeyPosted Aug 7, 2013, 2:26 AM EDT
Sir.. is there any other way to talk 2 u like gtalk or anything else?
Rohatash KumarTOP 100Posted Aug 7, 2013, 2:22 AM EDT
Saikat, if you have any problem related to sql server and .net. Please Feel free to ask me.
Saikat Kumar DeyPosted Aug 7, 2013, 2:15 AM EDT
ok sir...
Rohatash KumarTOP 100Posted Aug 7, 2013, 2:14 AM EDT
Saikat you can run directly using exec command on the page.
Saikat Kumar DeyPosted Aug 7, 2013, 2:05 AM EDT
In sql server.. actually i just passed 4th yr of btech n joined a company. so im learning of working with swql server stored procedure thats why.
Rohatash KumarTOP 100Posted Aug 7, 2013, 1:55 AM EDT
Saikat, You want that in asp.net page or SQL Server.
Saikat Kumar DeyPosted Aug 7, 2013, 1:41 AM EDT
i mean t say that if i want to show the update insert delte etc all four in one page then how to do it sir?
Saikat Kumar DeyPosted Aug 7, 2013, 1:37 AM EDT
u mean first insert code then go then update code then go like these?
Rohatash KumarTOP 100Posted Aug 7, 2013, 1:33 AM EDT
Saikit, you are executing more than one sql statement on the same page. so you can use go statement to separate SQL statements.
Saikat Kumar DeyPosted Aug 7, 2013, 1:23 AM EDT
Hello sir.. i implemented all the process and it done well.... thanx for such a awesome tools for the begineersbut i hav a question.. that is when i did the insert, update or delte in the same page where i made the procedure there its showing that commands executed succesfully but in the databse i cant find any change but iwhen im writing the same code in a new query option then its working.. can u plz help me in understood this?
Rohatash KumarTOP 100Posted Apr 1, 2013, 10:31 PM EDT
You can make @StatementType as out parameter. And use its string value in application.tue ngo tri
Tue NgoTriPosted Apr 1, 2013, 8:17 PM EDT
please help, I want to code your article. thanks sir
myintmyint htayPosted Dec 27, 2012, 9:46 PM EST
Thanks Sir:
roolerPosted Dec 14, 2012, 2:52 AM EST
I got error:Invalid object name 'MasterInsertUpdateDelete'. it happends when try to excecute alter table
Rohatash KumarTOP 100Posted Dec 3, 2012, 1:53 AM EST
Thanks Reza.
reza parmarPosted Dec 3, 2012, 1:29 AM EST
ok.
MattPosted Oct 10, 2012, 5:26 AM EDT
It is not a good idea to combine select/Insert/Update/Delete within a single stored procedure for multiple reasons. Firstly, you loose atomicity within your code; if you'd developed the code within a class you wouldn't have bundle all the logic into 1 method, you'd have 4 seperate ones. The same applies with a stored procedure. It's easier to unit test each procedure individually instead testing in a lump. It becomes a lot more difficult to maintain the code when the logic starts getting complex. @StatementType adds nothing to the overall operation other than a piece of data to make a decision on; you've made your call slower by virtue of having more data to pass and some conditional logic. If the calling application calls the relevant procedure (INSERT for instance) then there is no condition check to apply and you've not had to pass "INSERT" through the network. There are a whole host of locking issues where two people could try and update the same data at the same time, alternative update and delete. And a host of other reasons as to why this is not a good idea. Make database objects atomic as you would methods in a class - it's easier, cleaner, faster, and more maintainable.
Nomi MughalPosted Apr 2, 2012, 10:49 PM EDT
But how can I call this in my c# application.Kindly help me
vikash kumarPosted Apr 1, 2012, 1:09 PM EDT
THANKS PLZ TELL ME HOW TO USE IT IN ASP.NET OR HOW TO CALL THIS PROCEDURE IN ASP.NET
Kieran CallaghanPosted Dec 19, 2011, 6:38 AM EST
Nice article Rohatash very helpful to us beginners Kind Regards, Kieran