A stored procedure is nothing more than a prepared SQL code that you save so you can reuse the code over and over again. So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure.
“ SP is pre-define and pre-compiled set of souce code.”
Basic Difference between Stored Procedure and Functions
- Function must return a value, but in Stored Procedure it is optional (procedure can return zero or n values).
- Functions can have only input parameters for it, whereas procedures can have input/output parameters.
- Functions can be called from procedure, whereas procedures cannot be called from function.
- Procedure allows SELECT as well as DML (INSERT/UPDATE/DELETE) statement in it whereas function allows only SELECT statement in it.
- Stored procedure is precompiled execution plan whereas functions are not.
Note: Sometimes we face a question, why we can't execute stored procedure inside a function?
Answer:
- Stored Procedure may contain DML statements.
- Function can't contain DML statements.
So executing function inside stored procedure will never break rule 1, but executing stored procedure inside function may break rule 2.
So ultimately strict rule is made that we can't execute stored procedures inside function.
First we have to create one Table. E.g. Registration
- CREATE TABLE UserRegistration (
- C_Id int IDENTITY(1, 1) NOT NULL,
- C_Name varchar(100) NULL,
- C_Age varchar(100) NULL,
- C_Country varchar(100) NULL
- );
- Create procedure SpMyProcedure (
- @Id int = null,
- @Name varchar(100)= null,
- @Age varchar(100)= null,
- @Country varchar(100)= null,
- @Action varchar(100)= null
- ) As begin if @Action = 'Insert' Insert into UserRegistration(C_Name, C_Age, C_Country)
- values
- (@Name, @Age, @Country) if @Action = 'Update'
- Update
- UserRegistration
- set
- C_Name = @Name,
- C_Age = @Age,
- C_Country = @Countrywhere C_Id = @Id if @Action = 'Delete'
- Delete from
- UserRegistration
- where
- C_Id = @Id end
- protected void btnSave_Click(object sender, EventArgs e)
- {
- string str = "server=Your Server Name; Initial Catalog=Your Database Name; User ID=User Id; Password=Your Password";
- SqlConnection cn = new SqlConnection(str);
- SqlCommand cmd = new SqlCommand("SpMyProcedure", cn);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Action", "Insert");
- cmd.Parameters.AddWithValue("@Name", txtName.Text);
- cmd.Parameters.AddWithValue("@Age", txtAge.Text);
- cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
- cn.Open();
- cmd.ExecuteNonQuery();
- cn.Close();
- }
- protected void btnDelete_Click(object sender, EventArgs e)
- {
- string str = "server=Your Server Name; Initial Catalog=Your Database Name; User ID=User Id; Password=Your Password";
- SqlConnection cn = new SqlConnection(str);
- SqlCommand cmd = new SqlCommand("SpMyProcedure", cn);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Action", "Delete");
- cmd.Parameters.AddWithValue("@Id", txtId.Text);
- cn.Open();
- cmd.ExecuteNonQuery();
- cn.Close();
- }
- protected void btnUpdate_Click(object sender, EventArgs e)
- {
- string str = "server=Your Server Name; Initial Catalog=Your Database Name; User ID=User Id; Password=Your Password";
- SqlConnection cn = new SqlConnection(str);
- SqlCommand cmd = new SqlCommand("SpMyProcedure", cn);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Action", "Update");
- cmd.Parameters.AddWithValue("@Name", txtName.Text);
- cmd.Parameters.AddWithValue("@Age", txtAge.Text);
- cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
- cmd.Parameters.AddWithValue("@Id", txtId.Text);
- cn.Open();
- cmd.ExecuteNonQuery();
- cn.Close();
- }
- What is a Stored Procedure?
- How to create a stored procedure.?
- How to create a SQL Server stored procedure with parameters.?
- How to Insert, Update, Delete Using Stored Procedure in ASP.NET?


Lokesh NagojiPosted Sep 14, 2021, 7:06 AM EDT
Hello bro how to use grideview in sp
Ketan ShahPosted May 26, 2020, 9:30 PM EDT
Maximum stored procedure,Function, trigger or view nesting level exceeded( limit 32
Umesh DaiyaPosted Apr 8, 2020, 9:21 PM EDT
Before insert check C_Name already exists if exists show message duplicate else insert using store procedure
सुजाता चौहानPosted Nov 4, 2019, 10:07 PM EST
How to retrieve data form database using store procedure?
Ajay KrishPosted Oct 17, 2019, 3:52 AM EDT
For Delete procedure it shows error as we need to pass values for other parameters also.. So i have passed empty parameters and it has executed successfully.. but i need to know whether any other solution available without passing empty parameters.. Plz any one reply!! Thanks in Advance : )
asha pawarPosted Jul 23, 2018, 8:06 AM EDT
Without store procedure how we can perform crud operations in connected and disconnected architecture. when id is autoincremented and update delete data by using these id??/
Amit LelePosted Jul 6, 2017, 12:56 AM EDT
Thanks it's very helpful for beginners :)
Vikas GuptaPosted May 4, 2017, 9:10 PM EDT
A page can have only one server-side Form tag.
Teddy KurianPosted Jun 26, 2015, 1:24 AM EDT
Good start Elmrabah Abdelillah :)
Elmrabah AbdelillahPosted Jun 25, 2015, 4:05 AM EDT
thks
Debendra DashPosted Jun 21, 2015, 11:24 PM EDT
good start......