@Anil,這裏是SQL Server 2008中的代碼示例,這將有助於你得到你的目標acomplished:
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中如果handlded變量表你使用的是以前的版本,你應該可以創建臨時表(#Table)。 注2:數據數據類型可能因您所使用的SQL版本而異。
來源
2012-03-01 13:50:25
G21
它肯定看起來像你的兩個表在empID列上有關係。請澄清。 另外,「和binf它兩個網格」是什麼意思? – jroyce 2012-03-01 07:32:09
是的,他們有一個關係.... – SoftwareNerd 2012-03-01 07:38:20
請澄清你的文章,因爲它有相反的陳述:「請求和平衡跟蹤器,它沒有關係」 – jroyce 2012-03-01 07:41:02