使用事务 SQL 更新值并在新的 table 中插入更改
Update the values and insert the change in a new table using transactions SQL
我正在尝试创建一个新的历史记录 table,用于保存对 table TempLimitBook 所做的任何更新。
当执行下面的代码时,table TempLimitBookHistory (hsitorical table) 得到两条记录,即使任何值已经更新。
有没有人知道以下代码可能有什么问题?
IF EXISTS
(SELECT RecordId
FROM TempLimitBook
WHERE RecordId = @RecordId)
BEGIN
BEGIN TRAN
UPDATE TempLimitBook SET
LimitName= @LimitName,
LimitValue= @LimitValue,
GCN = @GCN,
watchListFlag = @watchListFlag,
nextWatchListReview = @nextWatchListReview,
TotalLimitCurrency = @TotalLimitCurrency,
TotalLimitAmount = @TotalLimitAmount,
totalLimitAMountSEK = @totalLimitAMountSEK,
sTCountryLimitTLCurrency = @sTCountryLimitTLCurrency,
sTCountryLimitSEK = @sTCountryLimitSEK,
mTCountryLimitTLCurrency = @mTCountryLimitTLCurrency,
mTCountryLimitSEK = @mTCountryLimitSEK,
riskClass = @riskClass,
accumulatedPolicyDeviationTLCurrency = @accumulatedPolicyDeviationTLCurrency,
accumulatedPolicyDeviationSEK = @accumulatedPolicyDeviationSEK,
transactionalPolicyDeviation = @transactionalPolicyDeviation,
transactionalPolicyDeviationSEK = @transactionalPolicyDeviationSEK,
forbearance = @forbearance,
nextReviewDate = @nextReviewDate,
country = @country,
segment = @segment,
profitCentre = @profitCentre,
clientExecutive = @clientExecutive,
accountManager = @accountManager,
industryCode = @industryCode,
active = @active,
decisionMakingBody = @decisionMakingBody
WHERE RecordId = @RecordId
INSERT INTO
TempLimitBookHistory (RecordId,LimitName,LimitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody, LastModifiedDate, LastModifiedUser)
select
recordId,limitName,limitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody, LastModifiedDate = CURRENT_TIMESTAMP, LastModifiedUser = SYSTEM_USER
from TempLimitBook
COMMIT TRAN
END
ELSE
INSERT INTO TempLimitBook (RecordId,LimitName,LimitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody)
VALUES
(@recordId,@limitName,@limitValue,@GCN,@watchListFlag,@nextWatchListReview,@TotalLimitCurrency,@totalLimitAmount,@totalLimitAMountSEK,@sTCountryLimitTLCurrency,@sTCountryLimitSEK,@mTCountryLimitTLCurrency,@mTCountryLimitSEK,@riskClass,@accumulatedPolicyDeviationTLCurrency,@accumulatedPolicyDeviationSEK,@transactionalPolicyDeviation,@transactionalPolicyDeviationSEK,@forbearance,@nextReviewDate,@country,@segment,@profitCentre,@clientExecutive,@accountManager,@industryCode,@active,@decisionMakingBody)
INSERT INTO
TempLimitBookHistory (RecordId,LimitName,LimitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody, LastModifiedDate, LastModifiedUser)
select
recordId,limitName,limitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody, LastModifiedDate = CURRENT_TIMESTAMP, LastModifiedUser = SYSTEM_USER
from TempLimitBook
END
在第一个翻译部分
INSERT INTO
TempLimitBookHistory (***)
select *
from TempLimitBook
您不区分您要插入的特定值,您从 TempLimitBook 中插入所有内容
在最后一部分中,您获取完整的 table 并将其插入到历史记录中table
这就是你想要的吗?
INSERT INTO
TempLimitBookHistory (*)
select
*
from TempLimitBook
并且您应该在更新值之前将其插入历史记录 table,否则如果您指定要插入的值,则会插入新数据。
我建议您阅读有关触发器安装的内容
然后它会在您更新 table 时自动执行此操作。
更新 TempLimitBook
然后将更改后的值插入 TempLimitBookHistory
我正在尝试创建一个新的历史记录 table,用于保存对 table TempLimitBook 所做的任何更新。
当执行下面的代码时,table TempLimitBookHistory (hsitorical table) 得到两条记录,即使任何值已经更新。
有没有人知道以下代码可能有什么问题?
IF EXISTS
(SELECT RecordId
FROM TempLimitBook
WHERE RecordId = @RecordId)
BEGIN
BEGIN TRAN
UPDATE TempLimitBook SET
LimitName= @LimitName,
LimitValue= @LimitValue,
GCN = @GCN,
watchListFlag = @watchListFlag,
nextWatchListReview = @nextWatchListReview,
TotalLimitCurrency = @TotalLimitCurrency,
TotalLimitAmount = @TotalLimitAmount,
totalLimitAMountSEK = @totalLimitAMountSEK,
sTCountryLimitTLCurrency = @sTCountryLimitTLCurrency,
sTCountryLimitSEK = @sTCountryLimitSEK,
mTCountryLimitTLCurrency = @mTCountryLimitTLCurrency,
mTCountryLimitSEK = @mTCountryLimitSEK,
riskClass = @riskClass,
accumulatedPolicyDeviationTLCurrency = @accumulatedPolicyDeviationTLCurrency,
accumulatedPolicyDeviationSEK = @accumulatedPolicyDeviationSEK,
transactionalPolicyDeviation = @transactionalPolicyDeviation,
transactionalPolicyDeviationSEK = @transactionalPolicyDeviationSEK,
forbearance = @forbearance,
nextReviewDate = @nextReviewDate,
country = @country,
segment = @segment,
profitCentre = @profitCentre,
clientExecutive = @clientExecutive,
accountManager = @accountManager,
industryCode = @industryCode,
active = @active,
decisionMakingBody = @decisionMakingBody
WHERE RecordId = @RecordId
INSERT INTO
TempLimitBookHistory (RecordId,LimitName,LimitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody, LastModifiedDate, LastModifiedUser)
select
recordId,limitName,limitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody, LastModifiedDate = CURRENT_TIMESTAMP, LastModifiedUser = SYSTEM_USER
from TempLimitBook
COMMIT TRAN
END
ELSE
INSERT INTO TempLimitBook (RecordId,LimitName,LimitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody)
VALUES
(@recordId,@limitName,@limitValue,@GCN,@watchListFlag,@nextWatchListReview,@TotalLimitCurrency,@totalLimitAmount,@totalLimitAMountSEK,@sTCountryLimitTLCurrency,@sTCountryLimitSEK,@mTCountryLimitTLCurrency,@mTCountryLimitSEK,@riskClass,@accumulatedPolicyDeviationTLCurrency,@accumulatedPolicyDeviationSEK,@transactionalPolicyDeviation,@transactionalPolicyDeviationSEK,@forbearance,@nextReviewDate,@country,@segment,@profitCentre,@clientExecutive,@accountManager,@industryCode,@active,@decisionMakingBody)
INSERT INTO
TempLimitBookHistory (RecordId,LimitName,LimitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody, LastModifiedDate, LastModifiedUser)
select
recordId,limitName,limitValue,GCN,watchListFlag,nextWatchListReview,TotalLimitCurrency,totalLimitAmount,totalLimitAMountSEK,sTCountryLimitTLCurrency,sTCountryLimitSEK,mTCountryLimitTLCurrency,mTCountryLimitSEK,riskClass,accumulatedPolicyDeviationTLCurrency,accumulatedPolicyDeviationSEK,transactionalPolicyDeviation,transactionalPolicyDeviationSEK,forbearance,nextReviewDate,country,segment,profitCentre,clientExecutive,accountManager,industryCode,active,decisionMakingBody, LastModifiedDate = CURRENT_TIMESTAMP, LastModifiedUser = SYSTEM_USER
from TempLimitBook
END
在第一个翻译部分
INSERT INTO
TempLimitBookHistory (***)
select *
from TempLimitBook
您不区分您要插入的特定值,您从 TempLimitBook 中插入所有内容
在最后一部分中,您获取完整的 table 并将其插入到历史记录中table 这就是你想要的吗?
INSERT INTO
TempLimitBookHistory (*)
select
*
from TempLimitBook
并且您应该在更新值之前将其插入历史记录 table,否则如果您指定要插入的值,则会插入新数据。
我建议您阅读有关触发器安装的内容 然后它会在您更新 table 时自动执行此操作。 更新 TempLimitBook 然后将更改后的值插入 TempLimitBookHistory