2
之外執行它來執行我有After Update
Update語句需要更長的時間比觸發
該觸發器的工作哪些是要鏈接的服務器
見代碼上更新2臺服務器上的一個觸發
ALTER TRIGGER [dbo].[tgAfterUpdate] ON [dbo].[KS_3EToVision]
AFTER UPDATE
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION -- Start the transaction
DECLARE @MatterNumber varchar(15);
SELECT @MatterNumber = i.MatterNumber FROM inserted i;
IF EXISTS(SELECT * FROM [FSSQLDEV01].[collnab].[dbo].[collection_header] WHERE Ch_file_number COLLATE DATABASE_DEFAULT = @MatterNumber COLLATE DATABASE_DEFAULT)
--UPDATE
IF (SELECT Ch_matter_status FROM [FSSQLDEV01].[collnab].[dbo].[collection_header] WHERE Ch_file_number COLLATE DATABASE_DEFAULT = @MatterNumber COLLATE DATABASE_DEFAULT) = 'Current'
BEGIN
--Parent table
UPDATE [FSSQLDEV01].[collnab].[dbo].[collection_header]
SET
Ch_start_date = i.OpenDate
FROM
Inserted i
WHERE ch_file_number COLLATE DATABASE_DEFAULT = @MatterNumber COLLATE DATABASE_DEFAULT
----Child table
UPDATE [FSSQLDEV01].[collnab].[dbo].[collections]
SET
Defendant_1 = i.Description
, 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(Collections.file_number,6) COLLATE DATABASE_DEFAULT = @MatterNumber COLLATE DATABASE_DEFAULT
END
--If we reach here, success!
COMMIT
END TRY
BEGIN CATCH
-- Whoops, there was an error
IF @@TRANCOUNT > 0
ROLLBACK
-- Raise an error with the details of the exception
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH
END
,如果我嘗試與觸發以外的相同標準的更新語句只會需要1秒但觸發它裏面最多可能需要45秒。
我已經縮小到第二個更新語句成爲問題,因爲如果我刪除第二個更新語句,它會很快執行。
我還附有圖片的顯示
我也有插入一個觸發器,這個工程很快內的第二和一個用於刪除,這將在觸發採取長時間使用,但是功能很好,如果我刪除觸發器之外的2條刪除語句。
USE [TE_3E_TRG]
GO
/****** Object: Trigger [dbo].[tgAfterDelete] Script Date: 08/30/2012 11:24:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tgAfterDelete] ON [dbo].[KS_3EToVision]
AFTER DELETE
AS
begin
BEGIN TRY
BEGIN TRANSACTION -- Start the transaction
Begin
--Parent table
DELETE FROM [FSSQLDEV01].[collnab].[dbo].[collection_header] WHERE ch_file_number COLLATE DATABASE_DEFAULT = (SELECT d.MatterNumber FROM deleted d) COLLATE DATABASE_DEFAULT
--Child table
DELETE FROM [FSSQLDEV01].[collnab].[dbo].[collections] WHERE Left(file_number,6) COLLATE DATABASE_DEFAULT = (SELECT d.MatterNumber FROM deleted d) COLLATE DATABASE_DEFAULT
END
-- If we reach here, success!
COMMIT
END TRY
BEGIN CATCH
-- Whoops, there was an error
IF @@TRANCOUNT > 0
ROLLBACK
-- Raise an error with the details of the exception
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH
有誰知道爲什麼在執行這個時間差急劇可以在執行觸發的觸發和出側中的語句之間發生的?
邊注:這一行'SELECT @MatterNumber = i.MatterNumber FROM inserted i;當你的'UPDATE'語句應該影響多於一行時''在你的觸發器的開頭就會失敗**可怕**。觸發器在每個語句**中被觸發**,並且在這種情況下,「插入」和「刪除」僞表都可能包含**多行**。你永遠不能依賴這樣一個事實:'Inserted'表只包含一行! –
可能的問題是遠程表在單個事務中被修改(這會變成分佈式事務),並且可能會增加一些開銷。儘管如此,我還沒有確定,並希望如果我完全錯誤,我會得到糾正。 –
當然,對WHERE子句中的列應用函數('LEFT()')的事實也沒有任何幫助。 –