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 employee
The 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
END
Now 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, 6:28 AM
How to call specific stored procedure. For example Exec Masterinsertupdatedelete where StatementType = 'Select'
Máÿàñk GhìldíÿâlPosted Feb 16, 2022, 3:49 PM
How to check select query in this stored procedure
roger simanjuntakPosted Dec 19, 2021, 5:29 PM
Msg 208, Level 16, State 6, Procedure Masterinsertupdatedelete, Line 2 [Batch Start Line 46]Invalid object name 'Masterinsertupdatedelete'.
Mohamed BassunyPosted Dec 8, 2020, 5:10 AM
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, 11:10 AM
Thanks this is perfect
Umesh DaiyaPosted Apr 9, 2020, 5:21 PM
In this how i can stop duplicate employee name
Chittaranjan SwainPosted Oct 30, 2019, 10:10 AM
Nice article...
Sandipta SahuPosted May 9, 2019, 12:22 AM
How to data-bind to grid view with parameter
Sandipta SahuPosted May 8, 2019, 11:35 PM
How to pass parameters for select comment help me/
Pramod PeyyalaPosted Jan 8, 2019, 1:29 AM
Nice Article
Piyush AgarwalPosted Mar 9, 2018, 5:59 AM
Thanks for sharing this...
Mohit SharmaPosted Jul 14, 2017, 3:54 AM
If i want to update the stored procedure table column not having any parameters than how it will ??
manish kumarPosted May 19, 2017, 10:50 AM
My query is not working Display Must Declare the Scalar variable
Sa HulPosted Mar 11, 2017, 3:22 AM
How to write condition in asp.net for (insert and update, delete) in storedprocedure
kalu singh raoPosted Jul 9, 2016, 10:39 AM
Nice...
Noor MowafaqPosted Dec 7, 2015, 4:31 AM
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 31, 2015, 3:13 AM
I search internet for 3 hour to find this page thank you
Imran AhmedPosted Jan 30, 2015, 11:51 AM
thanks a lot,
Alex MejikPosted Oct 22, 2014, 3:25 AM
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 22, 2014, 3:24 AM
from c$ i call it - db_contex.MasterInsertUpdateDelete_student(null,fname, lname, bdate, adress, phone,"Insert");
Alex MejikPosted Oct 22, 2014, 3:23 AM
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 14, 2014, 1:07 AM
nice sir .simply super
Shreya SoniPosted Aug 30, 2014, 4:15 AM
Its useful.. I need a common stored procedure to update all tables.. A common for all... Plz help me..
Aman PetwalPosted Feb 5, 2014, 10:52 AM
its verry helpfull thanx sir ji
Tariq HashmiPosted Jan 29, 2014, 5:57 AM
Thanks sir ji
nupur kadamPosted Nov 27, 2013, 7:43 AM
thanks
durgesh pandeyPosted Nov 5, 2013, 9:20 AM
Thanks!!!!!!!!!!!!! nice 1
Saikat Kumar DeyPosted Aug 7, 2013, 6:30 AM
thanx sir.. i invited u to chat.. my email id is [email protected]
Rohatash KumarPosted Aug 7, 2013, 6:28 AM
My Email-id- [email protected]
Saikat Kumar DeyPosted Aug 7, 2013, 6:26 AM
Sir.. is there any other way to talk 2 u like gtalk or anything else?
Rohatash KumarPosted Aug 7, 2013, 6:22 AM
Saikat, if you have any problem related to sql server and .net. Please Feel free to ask me.
Saikat Kumar DeyPosted Aug 7, 2013, 6:15 AM
ok sir...
Rohatash KumarPosted Aug 7, 2013, 6:14 AM
Saikat you can run directly using exec command on the page.
Saikat Kumar DeyPosted Aug 7, 2013, 6:05 AM
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 KumarPosted Aug 7, 2013, 5:55 AM
Saikat, You want that in asp.net page or SQL Server.
Saikat Kumar DeyPosted Aug 7, 2013, 5:41 AM
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, 5:37 AM
u mean first insert code then go then update code then go like these?
Rohatash KumarPosted Aug 7, 2013, 5:33 AM
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, 5:23 AM
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 KumarPosted Apr 2, 2013, 2:31 AM
You can make @StatementType as out parameter. And use its string value in application.tue ngo tri
Tue NgoTriPosted Apr 2, 2013, 12:17 AM
please help, I want to code your article. thanks sir
myintmyint htayPosted Dec 28, 2012, 2:46 AM
Thanks Sir:
roolerPosted Dec 14, 2012, 7:52 AM
I got error:Invalid object name 'MasterInsertUpdateDelete'. it happends when try to excecute alter table
Rohatash KumarPosted Dec 3, 2012, 6:53 AM
Thanks Reza.
reza parmarPosted Dec 3, 2012, 6:29 AM
ok.
MattPosted Oct 10, 2012, 9:26 AM
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 3, 2012, 2:49 AM
But how can I call this in my c# application.Kindly help me
vikash kumarPosted Apr 1, 2012, 5:09 PM
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, 11:38 AM
Nice article Rohatash very helpful to us beginners Kind Regards, Kieran