2012-03-01 44 views
1

我有我的數據庫Requests並沒有任何關係Balance跟蹤兩個表....但我想從兩個表中選擇數據,並將其綁定兩個格......如何使用單個存儲過程一次更新兩個表?

要求

​​

餘額跟蹤器

EmpId | BalanceAmnt | LastUpdated | lastApprovedAmount 
    1 | 5000  | sdfbk  | 
    2 | 3000  | sjbfsh  | 

現在我想基於EmpID一次更新兩個表。每當量被批准,就應該在request表列[ApprovedAmount]並且優先進行更新......

[ApprovedAmount]更新也應加入量批准更新[BalanceAmnt]平衡跟蹤,[LASTUPDATED],[lastApprovedAmount ]應與日期和時間進行更新

誰能幫我查詢請....

回答

0

首先創建一個存儲過程來更新您的[ApprovedAmount]

create procedure SP_UpdateRequestedAmount 
    (@EmpID int, 
    @AmountApproved varchar (50), 
    @RequestPriority varchar (50)) 
as 
begin 
    Update Requests 
    set ApprovedAmount = @AmountApproved, 
     Request_Priority = @RequestPriority 
    where 
     Emp_ID = @EmpID 
end 

然後用TRIGER更新其他表時曾經[AmountApproved]列被更新

CREATE TRIGGER tr_ApprovedAmount_UPDATE 
ON Requests 
AFTER UPDATE 
AS 
    --Make sure Priority was changed 
    IF NOT UPDATE(ApprovedAmount) 
     RETURN 

    --Determine if Priority was changed to high 

    -- PUT your queries for Updatating other table 
    declare @Balance_LastUpdated date 
    declare @ApprovedDate date 

    UPDATE Balance Tracker 
    SET Balance Tracker.Balance_BalanceAmount = Balance Tracker.Balance_BalanceAmount + PTS_Requests.Request_IsApproved, 
     Balance_LastUpdated = @Balance_LastUpdated, 
     Balance_LastApproval = @ApprovedDate 
    FROM 
     Balance Tracker 
    INNER JOIN 
     Requests ON Balance Tracker.Emp_ID = Requests.Emp_ID 

希望這有助於.........

0

請了扳機也試試這個

CREATE procedure SP_UpdateRequestedAmount 
    (
    @EmpID int, 
    @AmountApproved varchar (50), 
    @RequestPriority varchar (50) 
    ) 
    as 
    begin 

    --Declare a variable for know current ApprovedAmount (that is before updation) 
    Declare @CurrentApprovedAmount AS INT 

    --set current ApprovedAmount 
    SET @AmountApproved =(SELECT ApprovedAmount FROM Requests WHERE [email protected]) 

    --check is @CurrentApprovedAmount differ from @AmountApproved 
    IF (@[email protected]) 
     BEGIN 
       --Here we need to update Requests and BalanceTracker 
     END 
    ELSE 
     BEGIN 
      --only update Requests -- no change in AmountApproved 
     END 

    go 
相關問題