stored procedure

14
Stored Procedure By Deepak Sharma Software Developer

Upload: deepak-sharma

Post on 21-May-2015

430 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Stored procedure

Stored Procedure

By Deepak SharmaSoftware Developer

Page 2: Stored procedure

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.

Page 3: Stored procedure

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.

Page 4: 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

Page 5: 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

Page 6: Stored procedure

--¤ 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

Page 7: Stored procedure

--¤ 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

Page 8: Stored procedure

FOR VIEW ALL OF QUERY OF STORED PROCEDURE

• sp_helptext ProcDemo_select• sp_helptext ProcDemo_insert• sp_helptext ProcDemo_update• sp_helptext ProcDemo_delete

Page 9: Stored procedure

FOR DELETE STORED PROCEDURE

• drop proc ProcDemo_select• drop proc ProcDemo_insert• drop proc ProcDemo_update• drop proc ProcDemo_delete

Page 10: Stored procedure

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

Page 11: Stored procedure

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

Page 12: Stored procedure

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

Page 13: Stored procedure

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();

Page 14: Stored procedure

Thanks

By Deepak SharmaSoftware Developer