我有两个表abstractDetails和auditAbstractDetails。我有一个Web表单,一个人提交有关抽象(标题,背景,目标等)的详细信息,并将值插入abstractDetails表。
现在我已经写了一个触发器,所以每当一个人更新他的表并再次提交它时,它应该将新值填充到abstractDetails中,并且它应该在AuditabstractDetails表(标题,背景,目标)中填充旧值和新值。 .... auditTitle,auditBackground .....)
旧值应存储在auditabstractDetails表的title title,background等列下,并且新值将存储在auditTitle,auditBackground等列下。)
这是我的触发器:
USE [Abstract]
GO
/****** Object: Trigger [dbo].[trgAuditabstractDetails] Script Date: 09/28/2016 11:45:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[trgAuditabstractDetails]
ON [dbo].[abstractDetails]
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON
delete from AuditabstractDetails where Id IN (SELECT I.Id
FROM Inserted I);
insert into AuditabstractDetails(Id, abstractInfoId, title, background,
objective, design, result, Audittitle, Auditbackground,
Auditobjective, Auditdesign, Auditresult)
-- case 1: ID unchanged
SELECT I.Id, I.abstractInfoId, D.title, D.background, D.objective,
D.design, D.result,
I.title, I.background, I.objective, I.design, I.result
FROM Inserted I
JOIN Deleted D on I.Id=D.Id;
END
GO
之后,当我从后端更新abstractDetails表中的表单(标题,背景等)的值时(通过编辑abstractDetails表中的值来自DB),它是完美的。但是当我通过表单更新它并提交它时,它正在更新两列中的新值(title,auditTitle等..),它不会在特定列中存储旧值,而是在两列中存储新值
abstractDetails table
===========================================
Id |title | background | Result
-------------------------------------------
1 Abs1 backAbs2 resAbs2
2 Abs2 backAbs2 resAbs2
-------------------------------------------
Expected Result:
AuditabstractDetails table
===============================================
Id |title | background | Result | AuditTitle | auditBackground |auditResult
----------------------------------------------------------------------------
1 Abs1 backAbs2 resAbs2 Audit1 auditback1 auditres1
2 Abs2 backAbs2 resAbs2 Audit2 auditback2 auditres2
----------------------------------------------------------------------------
ActualResult:
AuditabstractDetails table
===============================================
Id |title | background | Result | AuditTitle | auditBackground |auditResult
----------------------------------------------------------------------------
1 Audit1 auditback1 auditres1 Audit1 auditback1 auditres1
2 Audit2 auditback2 auditres2 Audit2 auditback2 auditres2
----------------------------------------------------------------------------
需要帮助
答案 0 :(得分:0)
听起来问题不在于触发器,而在于保存您的数据。如果它在SQL Server中按预期工作但在UI进行调用时失败,那么您需要检查UI以查看正在进行的操作。 UI似乎不止一次更新记录。我会通过删除触发器的delete
部分并查看最终在审核表中有多少行来测试。