如何编写查询以一次更新两个表?

时间:2012-03-01 07:24:28

标签: sql-server-2008 c#-4.0

我在我的数据库中有两个表名为...请求和余额跟踪器没有关系....但我想从两个表中选择数据并将其分为两个网格......

               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     |

现在我想基于EmpID两个表一次更新......当金额被批准时,它应该是请求表列[ApprovedAmount]中的更新并具有优先级... 当[ApprovedAmount]更新时[BalanceAmnt]也应通过添加已批准的金额来更新余额跟踪器,[LastUpdated],[lastApprovedAmount]应更新日期和时间

任何人都可以帮我查询......

2 个答案:

答案 0 :(得分:1)

@Anil,这是SQL Server 2008代码的一个示例,它可以帮助您实现目标:

DECLARE @Requests TABLE
(
    EmpId int 
    , EmpRqsts nvarchar(50)
    , EmpDescription nvarchar(250)
    , ApproverID int
    , ApprovedAmount money 
    , RequestPriority int 
)

DECLARE @BalanceTracker TABLE
(
    EmpId int 
    , BalanceAmnt money 
    , LastUpdated datetime
    , lastApprovedAmount money
)

-- Insert data for testing
INSERT INTO @Requests VALUES
(
    1
    , 'Something here'
    , 'Some descriptio here'
    , 1
    , 100
    , 1
)

INSERT INTO @Requests VALUES
(
    2
    , 'Something here 2 '
    , 'Some descriptio here 3'
    , 1
    , 215
    , 2
)


INSERT INTO @BalanceTracker VALUES
(
    1
    , 5000
    , GETDATE() - 3
    , 310
)

INSERT INTO @BalanceTracker VALUES
(
    2
    , 3000
    , (GETDATE() - 1)
    , 98
)

-- Declare local variables
DECLARE
    @NewAmount money
    , @NewPriority int
    , @SelectedEmpId int

-- Assing values for example
SELECT @NewAmount = 1000
    , @SelectedEmpId = 1
    , @NewPriority = 5

-- Get the tables values pre - updates
SELECT  *
FROM    @Requests

SELECT  *
FROM    @BalanceTracker


BEGIN TRY

    -- Update the record with new ApprovedAmount and Request Priority
    UPDATE  @Requests
    SET ApprovedAmount = @NewAmount
        , RequestPriority = @NewPriority
    WHERE   EmpId = @SelectedEmpId

    -- If no error found then update BalanceAmnt trable
    IF (@@ERROR = 0)
    BEGIN TRY
        UPDATE  @BalanceTracker
        SET BalanceAmnt = (BalanceAmnt + @NewAmount)
            , LastUpdated = GETDATE()
            , lastApprovedAmount = @NewAmount
        WHERE   EmpId = @SelectedEmpId
    END TRY
    BEGIN CATCH
    PRINT N'Error found updating @BalanceTracker table: ' + ISNULL(LTRIM(STR(ERROR_NUMBER())) , N'Unknown Error' )
        + N', Message: ' + ISNULL ( ERROR_MESSAGE() , N'No Message' )
    END CATCH
END TRY
BEGIN CATCH
    PRINT N'Error found updating @Requests table: ' + ISNULL(LTRIM(STR(ERROR_NUMBER())) , N'Unknown Error' )
        + N', Message: ' + ISNULL ( ERROR_MESSAGE() , N'No Message' )
END CATCH

-- Get the tables values post - updates
SELECT  *
FROM    @Requests

SELECT  *
FROM    @BalanceTracker

注1 :@Table是由SQL Server 2008处理的变量表。如果您使用以前的版本,则应该能够创建临时表(#Table)。 注意2 :数据数据类型可能因您使用的SQL版本而异。

答案 1 :(得分:0)

你可以用触发器来做这种事情。这样,无论何时执行第一次更新,它都会自动执行您指定的其他更新。