连接表上的tsql update trigger1更新主表,主表update trigger2尝试更新主表中同一行上的其他列-失败

连接表上的tsql update trigger1更新主表,主表update trigger2尝试更新主表中同一行上的其他列-失败,tsql,triggers,Tsql,Triggers,[编辑:在这篇博文的底部添加了表DDL、示例数据和触发器DDL,但这似乎简化了,这打开了解雇的大门,显然使测试更容易] 所以我有两个表,Transaction和transactionjunc 在TransactionTransactionJunc中更新记录时,它会更新事务表中的两列或三列,即金额、未应用的金额From和/或未应用的金额To 事务表有一个更新触发器,用于响应这3列的更新。更新金额时,它会更新同一行的余额和余额未存款列,并更新客户表余额。更新AmountAppliedFrom/To列

[编辑:在这篇博文的底部添加了表DDL、示例数据和触发器DDL,但这似乎简化了,这打开了解雇的大门,显然使测试更容易]

所以我有两个表,Transaction和transactionjunc

在TransactionTransactionJunc中更新记录时,它会更新事务表中的两列或三列,即金额、未应用的金额From和/或未应用的金额To

事务表有一个更新触发器,用于响应这3列的更新。更新金额时,它会更新同一行的余额和余额未存款列,并更新客户表余额。更新AmountAppliedFrom/To列时,将更新同一行的余额列

因此,在显式事务中,如果更新TransactionJunc表中的一行,则SSMS的Messages窗口中会出现错误:

Msg 3930,16级,状态1,程序 TransactionTransactionJunc_AfterUpdate,第143行[批处理开始行] 109]当前事务无法提交,无法支持 写入日志文件的操作。回滚事务

在结果窗口中,我有以下内容:

ErrorNumber ErrorSeverity ErrorState ErrorLine ErrorProcedure 错误消息207 16 1 41事务\u更新后无效列名 “存款金额”

我认为这是一个并发问题,事务上的触发器正在尝试更新行,但它仍然被TransactionJunc表上的触发器锁定。我不确定。但是我知道结果窗口错误消息关于无效列名是错误的-名称是正确的,当由TransactionJunc表正在更新的相同列的SMSS sql更新触发时,通过事务更新触发器正确更新。这是一个错误和误导性的错误

如果禁用了TransactionTransactionJunc触发器(在创建它之前),并通过更新sql更新事务表中的相同列(在显式事务中),那么它就可以正常工作。它与TransactionTransactionJunc对Transactions表进行更新时的触发器有关,而不是我手动进行更新

我不知道如何解决这个问题。我确实希望这些在事务中发生,以保证业务数据的完整性。这些表之间存在PK/FK关系——因此不存在引用完整性问题——但如果只发生此过程的一部分,客户余额和事务余额可能会出现异常。在下面的代码中,我将FK定义删除到其他未涉及的表中。我也没有包括索引等等

谢谢

[添加了表DDL、示例数据和触发器DDL-删除了事务表上的附加FK]

表格和样本数据:

/****** Object:  Table [dbo].[Transaction]    Script Date: 9/17/2019 2:15:18 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Transaction](
    [ID] [bigint] IDENTITY(1,1) NOT NULL,
    [Active] [bit] NOT NULL,
    [SourceCustomerID] [bigint] NOT NULL,
    [CustomerID] [bigint] NOT NULL,
    [CustomerAccount] [nvarchar](32) NOT NULL,
    [TransactionTypeID] [bigint] NOT NULL,
    [TransactionReference] [nvarchar](32) NOT NULL,
    [PaymentTypeID] [bigint] NULL,
    [PaymentReference] [nvarchar](32) NOT NULL,
    [NSFReceipt] [bit] NOT NULL,
    [Title] [nvarchar](50) NOT NULL,
    [Date] [datetime] NOT NULL,
    [TermID] [bigint] NULL,
    [TermTitle] [nvarchar](50) NOT NULL,
    [DueInDays] [int] NOT NULL,
    [DueDate] [datetime] NOT NULL,
    [Name1] [nvarchar](50) NOT NULL,
    [Name2] [nvarchar](50) NOT NULL,
    [Name3] [nvarchar](50) NOT NULL,
    [Addr1] [nvarchar](50) NOT NULL,
    [Addr2] [nvarchar](50) NOT NULL,
    [City] [nvarchar](50) NOT NULL,
    [State] [nvarchar](2) NOT NULL,
    [ZIP] [nvarchar](5) NOT NULL,
    [Memo] [nvarchar](50) NOT NULL,
    [Amount] [decimal](19,4) NOT NULL,
    [AmountAppliedFrom] [decimal](19,4) NOT NULL,
    [AmountAppliedTo] [decimal](19,4) NOT NULL,
    [Balance] [decimal](19,4) NOT NULL,
    [AmountDeposited] [decimal](19,4) NOT NULL,
    [BalanceUndeposited] [decimal](19,4) NOT NULL,
    [SendByPrint] [bit] NOT NULL,
    [SendByEmail] [bit] NOT NULL,
    [DateCreated] [datetime] NOT NULL,
    [DateEdited] [datetime] NOT NULL,
 CONSTRAINT [PK_Transaction] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_Active]  DEFAULT ((1)) FOR [Active]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_NSFReceipt]  DEFAULT ((0)) FOR [NSFReceipt]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_Amount]  DEFAULT ((0)) FOR [Amount]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_AmountAppliedFrom]  DEFAULT ((0)) FOR [AmountAppliedFrom]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_AmountAppliedTo]  DEFAULT ((0)) FOR [AmountAppliedTo]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_Balance]  DEFAULT ((0)) FOR [Balance]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_AmountDeposited]  DEFAULT ((0)) FOR [AmountDeposited]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_BalanceUndeposited]  DEFAULT ((0)) FOR [BalanceUndeposited]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_DateCreated]  DEFAULT (sysdatetime()) FOR [DateCreated]
GO

ALTER TABLE [dbo].[Transaction] ADD  CONSTRAINT [DF_Transaction_DateEdited]  DEFAULT (sysdatetime()) FOR [DateEdited]
GO



/****** Object:  Table [dbo].[TransactionTransactionJunc]    Script Date: 9/17/2019 2:20:05 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TransactionTransactionJunc](
    [ID] [bigint] IDENTITY(1,1) NOT NULL,
    [TransactionAppliedFromID] [bigint] NOT NULL,
    [TransactionAppliedToID] [bigint] NOT NULL,
    [Amount] [decimal](19,4) NOT NULL,
    [DateCreated] [datetime] NOT NULL,
    [DateEdited] [datetime] NOT NULL,
 CONSTRAINT [PK_TransactionTransactionJunc] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[TransactionTransactionJunc] ADD  CONSTRAINT [DF_TransactionTransactionJunc_DateCreated]  DEFAULT (sysdatetime()) FOR [DateCreated]
GO

ALTER TABLE [dbo].[TransactionTransactionJunc] ADD  CONSTRAINT [DF_TransactionTransactionJunc_DateEdited]  DEFAULT (sysdatetime()) FOR [DateEdited]
GO

ALTER TABLE [dbo].[TransactionTransactionJunc]  WITH CHECK ADD  CONSTRAINT [FK_TransactionTransactionJunc_Transaction_TransactionAppliedFrom] FOREIGN KEY([TransactionAppliedFromID])
REFERENCES [dbo].[Transaction] ([ID])
GO

ALTER TABLE [dbo].[TransactionTransactionJunc] CHECK CONSTRAINT [FK_TransactionTransactionJunc_Transaction_TransactionAppliedFrom]
GO

ALTER TABLE [dbo].[TransactionTransactionJunc]  WITH CHECK ADD  CONSTRAINT [FK_TransactionTransactionJunc_Transaction_TransactionAppliedTo] FOREIGN KEY([TransactionAppliedToID])
REFERENCES [dbo].[Transaction] ([ID])
GO

ALTER TABLE [dbo].[TransactionTransactionJunc] CHECK CONSTRAINT [FK_TransactionTransactionJunc_Transaction_TransactionAppliedTo]
GO

/* Data */
SET IDENTITY_INSERT [dbo].[Transaction] ON 
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (35841, 1, 3, 7958, N'999999', 1, N'24552', NULL, N'', 0, N'EMS Fee 2018', CAST(N'2018-07-01T00:00:00.000' AS DateTime), 28, N'Net 62', 62, CAST(N'2018-09-01T00:00:00.000' AS DateTime), N'Sample Customer Name', N'', N'', N'1234 Fake St.', N'', N'Sample City', N'SS', N'99999', N'', 50.0000, 50.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1, 0, CAST(N'2018-06-13T20:11:11.000' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (35842, 1, 3, 7958, N'999999', 1, N'24553', NULL, N'', 0, N'EMS Fee 2017 - 1st Half', CAST(N'2017-07-01T00:00:00.000' AS DateTime), 28, N'Net 62', 62, CAST(N'2017-09-01T00:00:00.000' AS DateTime), N'Sample Customer Name', N'', N'', N'1234 Fake St.', N'', N'Sample City', N'SS', N'99999', N'', 25.0000, 25.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1, 0, CAST(N'2018-07-04T10:34:12.000' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (35843, 1, 3, 7958, N'999999', 1, N'24554', NULL, N'', 0, N'EMS Fee 2017', CAST(N'2017-07-01T00:00:00.000' AS DateTime), 29, N'Net 243', 243, CAST(N'2018-03-01T00:00:00.000' AS DateTime), N'Sample Customer Name', N'', N'', N'1234 Fake St.', N'', N'Sample City', N'SS', N'99999', N'VOID:', 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1, 0, CAST(N'2018-07-04T10:35:03.000' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (42598, 1, 3, 7958, N'999999', 3, N'FC 2', NULL, N'', 0, N'Late Fee 2018 - for EMS Fee 2018', CAST(N'2018-09-04T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2018-09-04T00:00:00.000' AS DateTime), N'Sample Customer Name', N'', N'', N'1234 Fake St.', N'', N'Sample City', N'SS', N'99999', N'Finance Charge', 15.0000, 15.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1, 0, CAST(N'2018-09-04T09:31:18.000' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (44963, 1, 3, 7958, N'999999', 3, N'FC 2366', NULL, N'', 0, N'Late Fee 2018 - for EMS Fee 2018', CAST(N'2018-09-24T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2018-09-24T00:00:00.000' AS DateTime), N'Sample Customer Name', N'', N'', N'1234 Fake St.', N'', N'Sample City', N'SS', N'99999', N'Finance Charge', 15.0000, 10.0000, 0.0000, 5.0000, 0.0000, 0.0000, 1, 0, CAST(N'2018-09-24T16:35:44.000' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (62197, 1, 3, 7958, N'999999', 7, N'', 7, N'', 0, N'Receipt-Cash', CAST(N'2018-10-01T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2018-10-01T00:00:00.000' AS DateTime), N'', N'', N'', N'', N'', N'', N'', N'', N'', 5.0000, 0.0000, 5.0000, 0.0000, 0.0000, 5.0000, 1, 0, CAST(N'2019-09-14T16:04:04.783' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (62215, 1, 3, 7958, N'999999', 7, N'', 7, N'', 0, N'Receipt-Cash', CAST(N'2018-10-01T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2018-10-01T00:00:00.000' AS DateTime), N'', N'', N'', N'', N'', N'', N'', N'', N'', 50.0000, 0.0000, 50.0000, 0.0000, 0.0000, 50.0000, 1, 0, CAST(N'2019-09-14T20:04:16.160' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (62216, 1, 3, 7958, N'999999', 7, N'', 7, N'', 0, N'Receipt-Cash', CAST(N'2018-10-02T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2018-10-02T00:00:00.000' AS DateTime), N'', N'', N'', N'', N'', N'', N'', N'', N'', 20.0000, 0.0000, 20.0000, 0.0000, 0.0000, 20.0000, 1, 0, CAST(N'2019-09-14T20:04:16.160' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (62217, 1, 3, 7958, N'999999', 7, N'', 7, N'', 0, N'Receipt-Cash', CAST(N'2018-10-03T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2018-10-03T00:00:00.000' AS DateTime), N'', N'', N'', N'', N'', N'', N'', N'', N'', 10.0000, 0.0000, 0.0000, 10.0000, 0.0000, 10.0000, 1, 0, CAST(N'2019-09-14T20:04:16.160' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (62252, 1, 3, 7958, N'999999', 7, N'', 7, N'', 0, N'Receipt-Cash', CAST(N'2019-01-01T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2019-01-01T00:00:00.000' AS DateTime), N'', N'', N'', N'', N'', N'', N'', N'', N'', 30.0000, 0.0000, 0.0000, 30.0000, 0.0000, 30.0000, 1, 0, CAST(N'2019-09-15T06:42:28.610' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (62253, 1, 3, 7958, N'999999', 7, N'', 7, N'', 0, N'Receipt-Cash', CAST(N'2019-08-02T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2019-08-02T00:00:00.000' AS DateTime), N'', N'', N'', N'', N'', N'', N'', N'', N'', 5.0000, 0.0000, 0.0000, 5.0000, 0.0000, 5.0000, 1, 0, CAST(N'2019-09-15T06:42:28.610' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
INSERT [dbo].[Transaction] ([ID], [Active], [SourceCustomerID], [CustomerID], [CustomerAccount], [TransactionTypeID], [TransactionReference], [PaymentTypeID], [PaymentReference], [NSFReceipt], [Title], [Date], [TermID], [TermTitle], [DueInDays], [DueDate], [Name1], [Name2], [Name3], [Addr1], [Addr2], [City], [State], [ZIP], [Memo], [Amount], [AmountAppliedFrom], [AmountAppliedTo], [Balance], [AmountDeposited], [BalanceUndeposited], [SendByPrint], [SendByEmail], [DateCreated], [DateEdited]) VALUES (62254, 1, 3, 7958, N'999999', 1, N'999001', NULL, N'', 0, N'EMS Fee 2019', CAST(N'2019-09-15T00:00:00.000' AS DateTime), NULL, N'', 0, CAST(N'2019-09-15T00:00:00.000' AS DateTime), N'Sample Customer Name', N'', N'', N'1234 Fake St.', N'', N'Sample City', N'SS', N'99999', N'testing', 50.0000, 0.0000, 0.0000, 50.0000, 0.0000, 50.0000, 1, 0, CAST(N'2019-09-15T06:42:28.610' AS DateTime), CAST(N'2019-09-17T08:19:13.527' AS DateTime))
GO
SET IDENTITY_INSERT [dbo].[Transaction] OFF
GO
SET IDENTITY_INSERT [dbo].[TransactionTransactionJunc] ON 
GO
INSERT [dbo].[TransactionTransactionJunc] ([ID], [TransactionAppliedFromID], [TransactionAppliedToID], [Amount], [DateCreated], [DateEdited]) VALUES (27300, 62215, 35841, 50.0000, CAST(N'2019-09-17T08:19:13.230' AS DateTime), CAST(N'2019-09-17T08:19:13.230' AS DateTime))
GO
INSERT [dbo].[TransactionTransactionJunc] ([ID], [TransactionAppliedFromID], [TransactionAppliedToID], [Amount], [DateCreated], [DateEdited]) VALUES (27301, 62197, 42598, 5.0000, CAST(N'2019-09-17T08:19:13.230' AS DateTime), CAST(N'2019-09-17T08:19:13.230' AS DateTime))
GO
INSERT [dbo].[TransactionTransactionJunc] ([ID], [TransactionAppliedFromID], [TransactionAppliedToID], [Amount], [DateCreated], [DateEdited]) VALUES (27302, 62216, 42598, 10.0000, CAST(N'2019-09-17T08:19:13.230' AS DateTime), CAST(N'2019-09-17T08:19:13.230' AS DateTime))
GO
INSERT [dbo].[TransactionTransactionJunc] ([ID], [TransactionAppliedFromID], [TransactionAppliedToID], [Amount], [DateCreated], [DateEdited]) VALUES (27303, 62216, 44963, 10.0000, CAST(N'2019-09-17T08:19:13.230' AS DateTime), CAST(N'2019-09-17T08:19:13.230' AS DateTime))
GO
SET IDENTITY_INSERT [dbo].[TransactionTransactionJunc] OFF
GO
触发因素:

/*  
    Trigger to Update the Balances in Transaction table when a Transaction is Updated - such as
    applying credit Transactions to debit Transactions and Deposits. Customer Balance is also updated.
*/

CREATE TRIGGER [dbo].[Transaction_AfterUpdate]
ON dbo.[Transaction]
AFTER Update
AS
IF (ROWCOUNT_BIG() = 0)  -- Must be the first statement. Even after set nocount on it will then return 0 and drop out of the trigger
RETURN;
set nocount on

begin try   
    begin tran
        /* Update the Transaction DateEdited column */
        UPDATE dbo.[Transaction]
        SET DateEdited = SYSDATETIME()
        FROM Inserted i
        WHERE dbo.[Transaction].ID = i.ID

        if (UPDATE([Amount]) or UPDATE([AmountAppliedFrom]) or UPDATE([AmountAppliedTo]) or UPDATE([AmountDeposited]))
            /* Calulate differences in the Amounts and store temporarily */  -- I think this is working fine - maybe needs more columns?
            select  i.*,
                (i.Amount - d.Amount) as AmountDiff,
                (d.AmountAppliedFrom - i.AmountAppliedFrom) as AmountAppliedFromDiff,   -- These are intentionally reversed (d-i vs i-d)
                (d.AmountAppliedTo - i.AmountAppliedTo) as AmountAppliedToDiff,         -- It made to logic cleaner below
                (d.AmountDeposited - i.AmountDeposited) as AmountDepositedDiff
            into    #tmp1
            from    inserted i join deleted d on i.ID = d.ID        

        if (UPDATE([Amount]))
            begin
                if ((select count(*) from #tmp1 where AmountDiff <> 0 and AmountDeposited > 0) > 0)
                    Throw 50000, 'Business Rule Error: Cannot change Receipt Amount when any portion of it has been Deposited', 1
                else
                    begin
                        update Customer
                        set Balance +=
                            (
                                select ISNULL(SUM(CASE WHEN t.TransactionTypeID in (1, 2, 3, 4) THEN t.AmountDiff when t.TransactionTypeID in (5, 6, 7) then t.AmountDiff * -1 else 0 END), 0)
                                from #tmp1 t where t.CustomerID = Customer.ID
                            );
                        update [Transaction]
                        set Balance += 
                            (
                                select isnull(sum(t.AmountDiff), 0)
                                from #tmp1 t
                                where t.ID = [Transaction].ID
                            ),
                            BalanceUndeposited +=
                            (
                                select isnull(sum(t.AmountDiff * (case when t.TransactionTypeID in (1, 2, 3, 4, 5, 6) then 0
                                when t.TransactionTypeID = 7 then 1 end)), 0)  -- Only Receipts need AmountDeposited and BalanceUndeposited
                                from #tmp1 t
                                where t.ID = [Transaction].ID
                            )
                    end
            end

        if (UPDATE([AmountAppliedFrom]) or UPDATE([AmountAppliedTo]))
            update [Transaction]
            set Balance +=  ((select sum(AmountAppliedFromDiff) from #tmp1 where ID = [Transaction].ID) + 
                             (select sum(AmountAppliedToDiff) from #tmp1 where ID = [Transaction].ID))
            from #tmp1 i
            where [Transaction].ID = i.ID
        if (UPDATE([AmountDeposited]))
            update [Transaction]
            set BalanceUndeposited += (select sum(AmountDepositedDiff) from #tmp1 where ID = [Transaction].ID)
            from #tmp1 i            
            where [Transaction].ID = i.ID
        drop table if exists #tmp1
    commit tran
end try
begin catch
    exec usp_Report_Error
    drop table if exists #tmp1
    IF (XACT_STATE()) = -1  
    BEGIN  
        PRINT  N'The transaction is in an uncommittable state.' +  
                'Rolling back transaction.'  
        ROLLBACK TRANSACTION;  
    END;  

    -- Test if the transaction is committable.  
    IF (XACT_STATE()) = 1  
    BEGIN  
        PRINT N'The transaction is committable.' +  
            'Committing transaction.'  
        COMMIT TRANSACTION;     
    END;        
end catch

也许,如果您将表的DDL和触发器的代码添加到您的问题中,我们将能够更好地提供帮助。样本数据也很有用。乍一看,“无效列名'AmountDeposted'。”听起来像是在引用没有合适上下文的列。完成-我试图避免希望这是一个我不知道的简单触发理论-只是为了确保,我没有简化数据-这是我一直在玩的真实代码和示例数据(有更多真实数据).我告诉你们,这与这个错误无关-这是错误的。具体地说,是的,存款金额是#tmp1的一部分,请查看提供#tmp1的选择。它以[select i.*,…]开始,因此它获取inserted中的所有列—其中包括作为事务表中的一列而存放的金额—它只是将差异列添加到inserted/Transaction表的所有列中。同样,在启用事务触发器的情况下—如果我运行“update[Transaction]设置Amount=45,其中ID=62215'它运行并正确修改所有它应该修改的列。如果我禁用事务更新触发器,并运行此代码“update TransactionTransactionJunc set Amount=45,其中ID=27300”,该代码将触发TransactionTransactionJunc更新触发器-它将更新与上一次更新完全相同的行/列。重新打开事务更新触发器并执行该操作,如果出现关于存储量的愚蠢[FALSE]错误。令人沮丧的是,这是在托管服务器上,我无法使用SQL Profiler连接以查看它,这可能会有所帮助。。。
/*  
    Trigger to Update the AmountAppliedFrom/To in the Transaction table when a TransactionTransactionJunc record is Updated
*/

CREATE TRIGGER [dbo].[TransactionTransactionJunc_AfterUpdate]
ON dbo.[TransactionTransactionJunc]
AFTER Update
AS
IF (ROWCOUNT_BIG() = 0)  -- Must be the first statement. Even after set nocount on it will then return 0 and drop out of the trigger
RETURN;
set nocount on

begin try       
    /* Update the TransactionTransactionJunc DateEdited column */
    UPDATE dbo.TransactionTransactionJunc
    SET DateEdited = SYSDATETIME()
    FROM Inserted i
    WHERE dbo.TransactionTransactionJunc.ID = i.ID

    if (UPDATE(Amount))
        /* Calulate differences in the Amount and store temporarily */
        select  i.*, (i.Amount - d.Amount) as AmountDiff
        into    #tmp1
        from    inserted i join deleted d on i.ID = d.ID        

    /* Amount change is handled differently when only the Amount changes verses when the TransactionAppliedFrom/To also changes */
    if (UPDATE([Amount]) and not UPDATE(TransactionAppliedFromID) and not UPDATE(TransactionAppliedToID))
        begin
            update  [Transaction]
            set     AmountAppliedTo +=
                (
                    select isnull(sum(t.AmountDiff), 0) 
                    from #tmp1 t
                    where [Transaction].ID = t.TransactionAppliedFromID
                ), AmountAppliedFrom +=
                (
                    select isnull(sum(t.AmountDiff), 0) 
                    from #tmp1 t
                    where [Transaction].ID = t.TransactionAppliedToID
                )
        end
    /* Change Transaction that was AppliedFromID to a different Transaction */
    if (UPDATE(TransactionAppliedFromID) and not UPDATE(TransactionAppliedToID))
        begin
            /* Undo the previous Transaction AmountAppliedTo */
            update  [Transaction]
            set     AmountAppliedTo -= -- Reversing here, so subtract
                (
                    select isnull(sum(d.Amount), 0)
                    from deleted d
                    where d.TransactionAppliedFromID = [Transaction].ID
                )
            /* Update with the replacement Transaction record */
            update  [Transaction]
            set     AmountAppliedTo += -- Adding to the newly referenced Transaction - so add
                (
                    select ISNULL(sum(i.Amount), 0)
                    from inserted i
                    where i.TransactionAppliedFromID = [Transaction].ID
                )
            /* If Amount also changed then also update the Transaction (TransactionID=AppliedToID) AmountAppliedFrom value */
            if (UPDATE(Amount))
                update  [Transaction]
                set     AmountAppliedFrom +=
                    (
                        select  isnull(sum(t.AmountDiff), 0)
                        from    #tmp1 t
                        where   t.TransactionAppliedToID = [Transaction].ID
                    )
        end
    /* Change Transaction that was AppliedToID to a different Transaction */
    if (UPDATE(TransactionAppliedToID) and not UPDATE(TransactionAppliedFromID))
        begin
            /* Undo the previous Transaction AmountAppliedFrom */
            update  [Transaction]
            set     AmountAppliedFrom -= -- Reversing here, so subtract
                (
                    select isnull(sum(d.Amount), 0)
                    from deleted d
                    where d.TransactionAppliedToID = [Transaction].ID
                )
            /* Update with the replacement Transaction record */
            update  [Transaction]
            set     AmountAppliedFrom += -- Adding to the newly referenced Transaction - so add
                (
                    select ISNULL(sum(i.Amount), 0)
                    from inserted i
                    where i.TransactionAppliedToID = [Transaction].ID
                )
            /* If Amount also changed then also update the Transaction (TransactionID=AppliedFromID) AmountAppliedFrom value */
            if (UPDATE(Amount))
                update  [Transaction]
                set     AmountAppliedTo +=
                    (
                        select  isnull(sum(t.AmountDiff), 0)
                        from    #tmp1 t
                        where   t.TransactionAppliedFromID = [Transaction].ID
                    )
        end
        /* Change Transaction that was AppliedToID and AppliedFromID to different Transactions */
        if (UPDATE(TransactionAppliedToID) and UPDATE(TransactionAppliedFromID))
        begin
            /* Undo the previous Transaction AmountAppliedFrom */
            update  [Transaction]
            set     AmountAppliedFrom -= -- Reversing here, so subtract
                (
                    select isnull(sum(d.Amount), 0)
                    from deleted d
                    where d.TransactionAppliedToID = [Transaction].ID
                )
            /* Undo the previous Transaction AmountAppliedTo */
            update  [Transaction]
            set     AmountAppliedTo -= -- Reversing here, so subtract
                (
                    select isnull(sum(d.Amount), 0)
                    from deleted d
                    where d.TransactionAppliedFromID = [Transaction].ID
                )

            /* Update with the replacement Transaction records */
            update  [Transaction]
            set     AmountAppliedFrom += -- Adding to the newly referenced Transaction - so add
                (
                    select ISNULL(sum(i.Amount), 0)
                    from inserted i
                    where i.TransactionAppliedToID = [Transaction].ID
                )               
            update  [Transaction]
            set     AmountAppliedTo += -- Adding to the newly referenced Transaction - so add
                (
                    select ISNULL(sum(i.Amount), 0)
                    from inserted i
                    where i.TransactionAppliedFromID = [Transaction].ID
                )
            /* If Amount also changed, in this case it doesn't matter. Previous records are not being adjusted,
                the updating of the replacement Transactions with the inserted.Amount takes care of it for both
                sides of the update */
        end
    drop table if exists #tmp1  
end try
begin catch
    exec usp_Report_Error
    drop table if exists #tmp1
    IF (XACT_STATE()) = -1  
    BEGIN  
        PRINT  N'The transaction is in an uncommittable state.' +  
                'Rolling back transaction.'  
        ROLLBACK TRANSACTION;  
    END;  

    -- Test if the transaction is committable.  
    IF (XACT_STATE()) = 1  
    BEGIN  
        PRINT N'The transaction is committable.' +  
            'Committing transaction.'  
        COMMIT TRANSACTION;     
    END;        
end catch