Stored procedure


Published on

What is Stored Procedure and how to use it on .aspx page?

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Stored procedure

  1. 1. Stored Procedure By Deepak Sharma Software Developer
  2. 2. What is Stored Procedure? • A stored procedure is a group of sql statements that has been created and stored in the database. • Stored procedure will accepts input parameters so that a single procedure can be used over network by several clients using different input data.
  3. 3. Benefit of Stored Procedure • Stored procedure will reduce network traffic and increase the performance. • If we modify stored procedure all clients will get the updated stored procedure.
  4. 4. Types of Stored Procedure 1. System Stored Procedure 2. User Defined Stored Procedure Types of User Defined Stored Procedure:- 1. Non Parameterized Stored Procedure 2. Parameterized Stored Procedure
  5. 5. --¤ USER DEFINED PROCEDURE-"NON PARAMETERIZED STORED PROCEDURE" Firstly we have to create table:- • create table contact(id int,name nvarchar(15),age int) • select * from contact • insert into contact select 1,'Deepak',23 union all select 2,'Shipra',23 union all select 3,'Richa',23 union all select 4,'Rashi',23
  6. 6. --¤ USER DEFINED PROCEDURE-"NON PARAMETERIZED STORED PROCEDURE" Create Procedure:- • create proc ProcDemo_select as select * from contact Here proc declare as a procedure and you can also write procedure. Fore execute this procedure :- • exec ProcDemo_select
  7. 7. --¤ USER DEFINED PROCEDURE - "PARAMETERIZED PROCEDURE" • create proc ProcDemo_insert(@id int, @name nvarchar(15), @age int) as insert into contact values(@id,@name,@age) • exec ProcDemo_insert 4,'Aryan',23 • --or • --ProcDemo_insert 5,'Hello',23 • exec ProcDemo_select • ------------------------------------------------------------------------------------------------------------------- • create proc ProcDemo_update(@id int,@name nvarchar(15), @age int) as update contact set name=@name, age=@age where id=@id • exec ProcDemo_update 4,'Radha',23 • exec ProcDemo_select • ------------------------------------------------------------------------------------------------------------------- • create proc ProcDemo_delete(@id int) as delete from contact where id=@id • exec ProcDemo_delete 4 • exec ProcDemo_select
  8. 8. FOR VIEW ALL OF QUERY OF STORED PROCEDURE • sp_helptext ProcDemo_select • sp_helptext ProcDemo_insert • sp_helptext ProcDemo_update • sp_helptext ProcDemo_delete
  9. 9. FOR DELETE STORED PROCEDURE • drop proc ProcDemo_select • drop proc ProcDemo_insert • drop proc ProcDemo_update • drop proc ProcDemo_delete
  10. 10. NAMING STRORED PROCEDURE OBJECT So some of these may be: • uspInsertPerson - insert a new person record • uspGetAccountBalance - get the balance of an account • uspGetOrderHistory - return list of orders
  11. 11. STORED PROCEDURE USING TRY & CATCH BLOCK-:- • CREATE PROCEDURE ProcDemo_TryCatch AS BEGIN TRY SELECT * from contact END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; END CATCH exec ProcDemo_TryCatch FOR CALCULATING AVERAGE • create proc ProcedureAverage as select AVG(id) as Average from contact • exec ProcedureAverage
  12. 12. USING STORED PROCEDURE WITH ASP.NET PAGE WITH TRANSACTION ON .ASPX PAGE-:- create table test(id int,name varchar(20)) select * from test insert test select 1,'Deepak' union all select 2,'Rashi' create proc sp_Test(@id int,@name varchar(20)) as begin try insert into test values(@id,@name) END TRY BEGIN CATCH SELECT ERROR_LINE(),ERROR_NUMBER(),ERROR_PROCEDURE(),ERROR_SEVERITY( ),ERROR_STATE(),ERROR_MESSAGE(); END CATCH drop proc sp_Test
  13. 13. allow a developer to work with transaction with two simple statement: ¤ Begin Transaction ¤ Commit Transaction ADO.NET TRANSACTION:- con.Open(); SqlTransaction trans; trans = con.BeginTransaction(); try { cmd = new SqlCommand("sp_Test", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = txtID.Text; cmd.Parameters.AddWithValue("@name", SqlDbType.VarChar).Value = txtName.Text.ToString(); cmd.Transaction = trans; cmd.ExecuteNonQuery(); trans.Commit(); } catch { trans.Rollback(); Label1.Text = "Insert Fail"; } con.Close(); con.Dispose();
  14. 14. Thanks By Deepak Sharma Software Developer