Update Two table with Single Store Procedure



To update Two or more Table We can use Single Store Procedure.It can be use as Same as we use a query .the thing only we need to do is to use in Store Procedure ,

Here we are updating two table at a time using Store Procedure.
Lets see how.In the normal update Query in Sql Server Looks like this


  
update Department set DepartmentName='IT ' where DepartmentID=5

  update Designation set DesignationName='Admin' where   DesignationID=3


To use this in Store Procedure we will convert this query in Store procedure Syntax




 Create Procedure  SP_UpdateRecord 

 As
 Begin

 update Department set DepartmentName='IT' where DepartmentID=5
 update Designation set DesignationName='Admin' where DesignationID=3
 end



It will Work as same the single update Query Works.We can also update the Table by passing parameter to it .
Lets See how


Create Procedure  SP_UpdateRecord 
( @DepartmentID int,
@DesignationID int,
@DepartmentName nvarchar(32),
DesignationName nvarchar(32))
 As
 Begin

 update Department set DepartmentName=@DepartmentName where DepartmentID=@DepartmentID
 update Designation set DesignationName=@DesignationNamewhere DesignationID=@DesignationID
 end

No comments:

Post a Comment