0

我在更新触发器中有这个查询

UPDATE [FSSQLPROD01].[collectionsDELETE].[dbo].[collections] 
SET 
    Defendant_1 = i.DisplayName 
    , Loan_Number_1 = i.Comments 
    , Client = i.KS_BookName 
    , Date_Instructed = i.OpenDate 
    , Person_Responsible_name = i.ResponsibleFeeEarnerName 
    , Person_Responsible_Email = i.ResponsibleFeeEarnerEmail 
    , Person_Acting_name = i.BillingFeeEarnerName 
    , Person_Acting_email = i.BillingFeeEarnerEmail 
    , Agent_Acting_name = i.BillingFeeEarnerName 
    , Agent_Acting_email = i.BillingFeeEarnerEmail 
    , CBA_Panel_Service_Area = i.KS_ServiceCat 
    , HBN_Number = i.KS_ClientAcctRef 
    , St_George_Contact = i.KS_Instructor 
FROM 
    [FSSQLPROD01].[collectionsDELETE].[dbo].[collections] c
INNER JOIN 
    Inserted i
ON
    Left(c.file_number, 6) COLLATE DATABASE_DEFAULT = i.MatterNumber COLLATE DATABASE_DEFAULT

在此处输入图像描述 现在 Join 连接在 File_Number 上匹配,例如 70003-1 但由于某种未知原因,应该更新到此行的数据已更新为完全随机的行 614864-7

我可以继续执行触发器并更新 70003-1 行,但我继续更新另一行 614864-7

这也在链接服务器上运行,触发器在一个服务器上与其他服务器约会

服务器 tigger 位于 Microsoft SQL Server 2005 - 9.00.3042.00 (X64) Feb 10 2007 00:59:02 版权所有 (c) 1988-2005 Microsoft Corporation Enterprise Edition(64 位)在 Windows NT 5.2 上(Build 3790:Service Pack 2 )

服务器触发器正在更新 Microsoft SQL Server 2005 - 9.00.3042.00 (X64) Feb 10 2007 00:59:02 版权所有 (c) 1988-2005 Microsoft Corporation Enterprise Edition (64-bit) o​​n Windows NT 5.2 (Build 3790: Service Pack 2 )

我一直在想,将服务器 SP 更新到 SP4 可以解决问题吗?

有没有人知道为什么这可能会影响服务器

修正案

是的,我实际上对值进行了硬编码并简化了它应该工作的 SQL 语句

DECLARE @MatterNumber as varchar(50)
SET @MatterNumber = (SELECT i.MAtternumber FRom Inserted as i) + '-1'
Print @MatterNumber

UPDATE [FSSQLPROD01].[collectionsDELETE].[dbo].[collections] 
       SET Loan_Number_1 = ‘123’
       WHERE file_number =  @MatterNumber

但是,一旦我将 Declared Varible 值设置为 Loan_Number_1 或任何其他列,这就是问题开始发生的时候

DECLARE @LoanNumber as varchar(50)
SET @LoanNumber =    (SELECT i.Comments FRom Inserted as i)
    Print @LoanNumber
DECLARE @MatterNumber as varchar(50)
SET @MatterNumber = (SELECT i.MAtternumber FRom Inserted as i) + '-1'

Print @MatterNumber –-I print here I see 70003-1

UPDATE [FSSQLPROD01].[collectionsDELETE].[dbo].[collections] 
       SET Loan_Number_1 = @LoanNumber

       WHERE file_number =  @MatterNumber

Print @MatterNumber –-I print here I see 70003-1
4

2 回答 2

0
UPDATE [FSSQLPROD01].[collectionsDELETE].[dbo].[collections] 
SET 
    Defendant_1 = i.DisplayName 
    , Loan_Number_1 = i.Comments 
    , Client = i.KS_BookName 
    , Date_Instructed = i.OpenDate 
    , Person_Responsible_name = i.ResponsibleFeeEarnerName 
    , Person_Responsible_Email = i.ResponsibleFeeEarnerEmail 
    , Person_Acting_name = i.BillingFeeEarnerName 
    , Person_Acting_email = i.BillingFeeEarnerEmail 
    , Agent_Acting_name = i.BillingFeeEarnerName 
    , Agent_Acting_email = i.BillingFeeEarnerEmail 
    , CBA_Panel_Service_Area = i.KS_ServiceCat 
    , HBN_Number = i.KS_ClientAcctRef 
    , St_George_Contact = i.KS_Instructor 
FROM

    Inserted i
where
    Left([FSSQLPROD01].[collectionsDELETE].[dbo].[collections] .file_number, 6) COLLATE DATABASE_DEFAULT = i.MatterNumber COLLATE DATABASE_DEFAULT
于 2012-11-08T07:16:32.267 回答
-4

触发器用于更新单个记录。当我们进行批量插入/更新时,它们不起作用。因为SQL Server triggers runs only 1 time per UPDATE. 如果您在单个语句中修改 3000 行,您的触发器将不会触发 3000 次。您的触发器只会触发一次。所以这就是它更新随机行的原因(尽管它应该是第一行)。

于 2012-11-08T03:41:35.920 回答