In database, you need to do Insert, Update and Delete. If we want to make a reliable and high performance system then these four operations must be implemented by stored procedures. Stored procedure also prevents Sql Injection attacks and reduce network traffic.

Insert Operation

We can insert records into the table(s) using stored procedure by passing data in input parameters. Below code is used to insert record in the table "Employee" using stored procedure
  1. CREATE TABLE Employee
  2. (
  3. EmpID int primary key, Name varchar(50),
  4. Salary int,
  5. Address varchar(100)
  6. )
  1. Insert into Employee(EmpID,Name,Salary,Address) Values(1,'Mohan',16000,'Delhi')
  2. Insert into Employee(EmpID,Name,Salary,Address) Values(2,'Asif',15000,'Delhi')
  3. Insert into Employee(EmpID,Name,Salary,Address) Values(3,'Bhuvnesh',19000,'Noida')
  4. --See table
  5. SELECT * FROM Employee
  1. CREATE PROCEDURE usp_InsertEmployee
  2. @flag bit output,-- return 0 for fail,1 for success
  3. @EmpID int,
  4. @Name varchar(50),
  5. @Salary int,
  6. @Address varchar(100)
  7. AS
  8. BEGIN
  9. BEGIN TRANSACTION
  10. BEGIN TRY
  11. Insert into Employee(EmpID,Name,Salary,Address) Values(@EmpID,@Name,@Salary,@Address)
  12. set @flag=1;
  13. IF @@TRANCOUNT > 0
  14. BEGIN commit TRANSACTION;
  15. END
  16. END TRY
  17. BEGIN CATCH
  18. IF @@TRANCOUNT > 0
  19. BEGIN rollback TRANSACTION;
  20. END
  21. set @flag=0;
  22. END CATCH
  23. END
  1. --Execute above created procedure to insert rows into table
  2. Declare @flag bit
  3. EXEC usp_InsertEmployee @flag output,1,'Deepak',14000,'Noida'
  4. if @flag=1
  5. print 'Successfully inserted'
  6. else
  7. print 'There is some error'
  1. --Execute above created procedure to insert rows into table
  2. Declare @flag bit
  3. EXEC usp_InsertEmployee @flag output,4,'Deepak',14000,'Noida'
  4. if @flag=1
  5. print 'Successfully inserted'
  6. else
  7. print 'There is some error'
  1. --now see modified table
  2. Select * from Employee

Retrieve Operation

We can retrieve data from one or more tables/views with the help of join, using stored procedure. We can put multiple sql statements with in a single stored procedure. Below code is used to fetch data from a table "Employee" using stored procedure
  1. -- first we Insert data in the table
  2. Insert into Employee(EmpID,Name,Salary,Address) Values(1,'Mohan',16000,'Delhi')
  3. Insert into Employee(EmpID,Name,Salary,Address) Values(2,'Asif',15000,'Delhi')
  4. Insert into Employee(EmpID,Name,Salary,Address) Values(3,'Bhuvnesh',19000,'Noida')
  5. go
  6. --Now we create a procedure to fetch data
  7. CREATE PROCEDURE usp_SelectEmployee
  8. As
  9. Select * from Employee ORDER By EmpID
  1. --Execute above created procedure to fetch data
  2. exec usp_SelectEmployee

Update Operation

We can update records of the table(s) using stored procedure by passing data in input parameters. Below code is used to update a table "Employee" using stored procedure
  1. CREATE PROCEDURE usp_UpdateEmployee
  2. @flag bit output,-- return 0 for fail,1 for success
  3. @EmpID int,
  4. @Salary int,
  5. @Address varchar(100)
  6. AS
  7. BEGIN
  8. BEGIN TRANSACTION
  9. BEGIN TRY
  10. Update Employee set Salary=@Salary, Address=@Address
  11. Where EmpID=@EmpID
  12. set @flag=1;
  13. IF @@TRANCOUNT > 0
  14. BEGIN commit TRANSACTION;
  15. END
  16. END TRY
  17. BEGIN CATCH
  18. IF @@TRANCOUNT > 0
  19. BEGIN rollback TRANSACTION;
  20. END
  21. set @flag=0;
  22. END CATCH
  23. END
  1. --Execute above created procedure to update table
  2. Declare @flag bit
  3. EXEC usp_UpdateEmployee @flag output,1,22000,'Noida'
  4. if @flag=1 print 'Successfully updated'
  5. else
  6. print 'There is some error'
  1. --now see updated table
  2. Select * from Employee

Delete Operation

We can delete records of the table(s) using stored procedure by passing data in input parameters. Below code is used to update a table "Employee" using stored procedure
  1. CREATE PROCEDURE usp_DeleteEmployee
  2. @flag bit output,-- return 0 for fail,1 for success
  3. @EmpID int
  4. AS
  5. BEGIN
  6. BEGIN TRANSACTION
  7. BEGIN TRY
  8. Delete from Employee Where EmpID=@EmpID set @flag=1;
  9. IF @@TRANCOUNT > 0
  10. BEGIN commit TRANSACTION;
  11. END
  12. END TRY
  13. BEGIN CATCH
  14. IF @@TRANCOUNT > 0
  15. BEGIN rollback TRANSACTION;
  16. END
  17. set @flag=0;
  18. END CATCH
  19. END
  1. --Execute above created procedure to delete rows from table
  2. Declare @flag bit
  3. EXEC usp_DeleteEmployee @flag output, 4
  4. if @flag=1
  5. print 'Successfully deleted'
  6. else
  7. print 'There is some error'
  1. --now see modified table
  2. Select * from Employee

Note

  1. In stored procedure we use output parameter to return multiple values.
  2. Generally we use output parameter in stored procedure to get status of the operation as I used above "@flag" output parameter to get operations status whether these are successfully executed or not.
Summary
In this article I try to explain basic Insert, Retrieve, Update, Delete Operations using Stored Procedures. I hope after reading this article you will be know how to implement these operations using stored procedure. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

0 comments:

Post a Comment

 
Top