1

I have two tables in my database Requests and Balance tracker which has no relation.... but I want to select data from two tables and bind it two grid...

Requests

EmpID | EmpRqsts | EmpDescription | ApproverID | ApprovedAmount | RequestPriority
   1  | asdfsb   | sadbfsbdf      |     1      |
   2  | asbfd    | sjkfbsd        |     1      |

Balance Tracker

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

Now I want to update both tables at a time, based on the EmpID. Whenever amount is approved, it should be updated in request table column [ApprovedAmount] and with priority...

When [ApprovedAmount] is updated [BalanceAmnt] Balance Tracker of also should be Updated by adding the amount approved,[LastUpdated],[lastApprovedAmount] should be updated with date and time

Can anyone help me with the query please....

2 Answers 2

0

First create a stored procedure to update your [ApprovedAmount] column

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

Then use a triger to update the other table when ever [AmountApproved] column is updated

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

Hope this helps.........

0
0

Please try this also with out trigger

    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 EmpID=@EmpID)

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

   go

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.