Skip to main content

What is Stored Procedure?


A stored procedure is a pre-compiled group of Transact-SQL statements .We can say a stored procedure is a prepared SQL code that we save so that we can reuse the code over and over again. If a repetitive T-SQL task has to be executed within an application, then the best way for it is to create stored procedure.
It is always recommended to create Stored Procedure instead of writing Inline queries so that we can just call the Stored Procedures whenever required instead of writing Inline queries again and again each time.
You can also pass parameters to the stored procedure, so depending on what the need is the stored procedure can act accordingly based on the parameter values that were passed to it.
Implementation:
Let's create the table and the basic stored procedure to perform Save, Update, Delete, Bind and Search operation on Sql server database table.
First of all create a database in Sql server and name it "BooksDb" or whatever you want.
Column Name datatype
BookId Int(Primary Key and set Is Identity=true)
BookName varchar(100)
Author varchar(100)
Publisher varchar(200)
Price decimal(18, 2)
Create a table with the columns and data type as shown above and name it "tbBooks" using the script below.
CREATE TABLE tbBooks
(
BookId int IDENTITY(1,1) NOT NULL,
BookName varchar(100) NULL,
Author varchar(100) NULL,
Publisher varchar(200) NULL,
Price decimal(18, 2) NOT NULL
)
Now let's create the basic stored procedure to perform Save, Update, Delete, Bind and Search operation on Sql server database table.
>> Stored procedure to insert book details in tbBooks table
CREATE PROCEDURE InsertBookDetails_Sp
@BookName VARCHAR(100),
@Author VARCHAR(100),
@Publisher VARCHAR(200),
@Price DECIMAL(18,2),
AS
BEGIN
INSERT INTO tbBooks
(BookName,Author,Publisher,Price)
VALUES
(@BookName,@Author,@Publisher,@Price)
END
>> Stored procedure to update book details in tbBooks table
CREATE PROCEDURE UpdateBookRecords_Sp
(
@BookId INT,
@BookName VARCHAR(100),
@Author VARCHAR(100),
@Publisher VARCHAR(200),
@Price DECIMAL(18,2)
)
AS
BEGIN
UPDATE tbBooks SET
BookName=@BookName,
Author=@Author,
Publisher=@Publisher,
Price=@Price
WHERE BookId=@BookId
END
>> Stored procedure to delete book details in tbBooks table
CREATE PROCEDURE DeleteBookRecords_Sp
(
@BookId INT
)
AS
BEGIN
DELETE FROM tbBooks WHERE BookId=@BookId
END
>> Stored procedure to get the records from tbBooks table to bind in any data control e.g. GridView, DataList, Repeater etc.
CREATE PROCEDURE BindBookDetails_Sp
AS
BEGIN
SELECT * FROM tbBooks
END
>> Stored procedure to search any book based on BookId from tbBooks table
CREATE PROCEDURE SearchBookRecord_Sp
(
@BookId int
)
AS
Begin
SELECT * FROM tbBooks WHERE @BookId=@BookId
End

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

First, FirstOrDefault, Single, SingleOrDefault In C#

For people who are new to LINQ, it is difficult to understand the difference between First, FirstOrDefault, Single, SingleOrDefault. In this blog, I will explain what to use and when.     I will take a simple example to make you understand practically how these methods work.   Consider a class Employee with properties as Id, Name, and Department. class  Employee {    public   int  Id {  get ;  set ; }    public   string  Name {  get ;  set ; }    public   string  Department{  get ;  set ; } } I have a list of Employees: List<Employee> employeeList =  new  List<Employee>(){    new  Employee() { Id = 1, Name =  "Sunny" , Department =  "Technical"  },    new  Employee() { Id=2, Name= "Pinki" , Department = "HR" },    new  Employee() { Id=3, Name= "Tensy" , De...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...