Download - Stored procedure
Stored Procedure
By Deepak SharmaSoftware Developer
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.
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.
Types of Stored Procedure
1. System Stored Procedure2. User Defined Stored Procedure
Types of User Defined Stored Procedure:-3. Non Parameterized Stored Procedure4. Parameterized Stored Procedure
--¤ 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
--¤ USER DEFINED PROCEDURE-"NON PARAMETERIZED STORED PROCEDURE"
Create Procedure:-• create proc ProcDemo_select as select * from contactHere proc declare as a procedure and you can also write procedure.Fore execute this procedure :-• exec ProcDemo_select
--¤ 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
FOR VIEW ALL OF QUERY OF STORED PROCEDURE
• sp_helptext ProcDemo_select• sp_helptext ProcDemo_insert• sp_helptext ProcDemo_update• sp_helptext ProcDemo_delete
FOR DELETE STORED PROCEDURE
• drop proc ProcDemo_select• drop proc ProcDemo_insert• drop proc ProcDemo_update• drop proc ProcDemo_delete
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
STORED PROCEDURE USING TRY & CATCH BLOCK-:-• CREATE PROCEDURE ProcDemo_TryCatch AS BEGIN TRY SELECT * from contactEND TRYBEGIN 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 CATCHexec ProcDemo_TryCatch
FOR CALCULATING AVERAGE
• create proc ProcedureAverage as select AVG(id) as Average from contact• exec ProcedureAverage
USING STORED PROCEDURE WITH ASP.NET PAGE WITH TRANSACTION ON .ASPX PAGE-:-
create table test(id int,name varchar(20))select * from testinsert test select 1,'Deepak' union all select 2,'Rashi'
create proc sp_Test(@id int,@name varchar(20)) as begin tryinsert into test values(@id,@name)END TRYBEGIN CATCHSELECT
ERROR_LINE(),ERROR_NUMBER(),ERROR_PROCEDURE(),ERROR_SEVERITY(),ERROR_STATE(),ERROR_MESSAGE();
END CATCHdrop proc sp_Test
allow a developer to work with transaction with two simple statement:¤ Begin Transaction¤ Commit TransactionADO.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();
Thanks
By Deepak SharmaSoftware Developer