Sample store procedure to update multi-table in SQL Server 2008


 Now I would like to show how to  update data in multi-table by ID field.This example below, I have 4 tables. I created  database diagram already by connected relationship 4 tables to each other.
Figure 1:  Sample four tables 
Database Diagram:
Figure 2:  Sample database diagram
Stored Procedure
create procedure sp_updatePro
@cate_name varchar(50),
@country varchar(50),
@username nvarchar(50),
@pro_name varchar(50),
@amount int ,
@proID int
as
--declare variable
declare @countryID int , @cateID int , @userID int
--set variable
set @countryID=(select countryid from tblcountry where country=@country)
set @cateID =(select CateID from TblCategory where CateName = @cate_name)
set @userID=(select userid from tbluser where name=@username)
update TblPro set userid = @userID , countryid = @countryID , CateID=@cateID , proName=@pro_name , Amount = @amount

Done....Just click on Execute to produce a query.
Sample store procedure to update multi-table in SQL Server 2008 Sample store procedure to update multi-table in SQL Server 2008 Reviewed by BeiLover on 1:41 PM Rating: 5

No comments:

Powered by Blogger.