@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