0

我在 dbo.Aritcle 表中有一个名为“updateFriendlyURLTitle”的触发器。当插入单个文章时,该触发器工作正常。

但在文章导入过程中:我使用了以下代码。此代码复制文章,但不会触发生成 FriendlyUrl 的触发器。

private void WriteArticlesToDatabase<TData>(DataSet ds, SqlTableDetails tableDetails, IEnumerable<TData> newArticles, SqlTransaction transaction)
{
    var dt = WriteToDataTable(ds, tableDetails.Table, newArticles);
    using (var bulkCopy = new SqlBulkCopy(_destConnection, SqlBulkCopyOptions.FireTriggers, transaction))
    {
        bulkCopy.DestinationTableName = tableDetails.ToString();
        bulkCopy.WriteToServer(dt);
    }
}

我的触发器如下:

ALTER TRIGGER [dbo].[updateFriendlyURLTitle] ON [dbo].[Articles] 
AFTER INSERT, UPDATE
AS

IF @@ROWCOUNT > 0
BEGIN
    IF  COLUMNS_UPDATED() != 0x0000200000100000 -- columns other than newsCounterViews have been updated
    BEGIN
        DECLARE @oldestfulllucenebuild AS DATETIME
        DECLARE @deletedNewsID INT
        DECLARE @newsStatus BIT
        DECLARE @maxcalcimp AS FLOAT
        DECLARE @insertedCalculatedImportance INT
        DECLARE @insertedNormalisedCalculatedImportance INT
        DECLARE @insertedSeoURLTitle VARCHAR(255)

        select @oldestfulllucenebuild = min(luceneIndexCreatedDate)
        from Lucene_Indexes
        where luceneIndexType = 'news'

        select @oldestfulllucenebuild = dateAdd(year,10,@oldestfulllucenebuild)

        select @maxcalcimp = cast(@oldestfulllucenebuild as float) * 48 *100 --the max importance

        select  @insertedCalculatedImportance = inserted.newsCalculatedImportance,
                 @insertedNormalisedCalculatedImportance = inserted.newsNormalisedCalculatedImportance,
                 @insertedSeoURLTitle = inserted.newsSeoURLTitle
        from inserted


        --if the current statement is updating the importance or seo columns then do not perform this query (so it doesn't get stuck in a loop)
        IF (NOT UPDATE(newsCalculatedImportance)) AND (NOT UPDATE(newsNormalisedCalculatedImportance)) AND (NOT UPDATE(newsSeoURLTitle)) 
            OR
        --if it is inserting a new record then perform the query
            (@insertedCalculatedImportance = 0 AND  @insertedNormalisedCalculatedImportance is null AND @insertedSeoURLTitle = '')
        BEGIN
            update Articles
            set newsCalculatedImportance = cast(cast(inserted.newsArticledate as float )*48 + inserted.newsimportance AS int)
            , newsNormalisedCalculatedImportance = (1/ @maxcalcimp) * cast(cast(inserted.newsArticledate as float )*48 + inserted.newsimportance AS int)
            , newsSeoURLTitle = LEFT(dbo.getSEOURLTitle(inserted.newstitle), 255)
            from Articles inner join inserted on
             Articles.newsid = inserted.newsid
        END

        SELECT @deletedNewsID = newsID, @newsStatus = newsStatus
        FROM inserted

        IF(@newsStatus = 0)
        BEGIN
            DELETE FROM tbl_DenormalisedNews WHERE newsid = @deletedNewsID

            DELETE FROM News_Deleted_DateTime
            WHERE NewsID = @deletedNewsID

            INSERT INTO News_Deleted_DateTime
            VALUES (@deletedNewsID, getDate())
        END
        ELSE
        BEGIN
            --news status is 1, remove it from the news_deleted_datetime table if it exists
            DELETE FROM News_Deleted_DateTime
            WHERE NewsID = @deletedNewsID
        END

        -- newsImage1 optimisation: if newsImage1 = '' THEN has_image = FALSE ELSE has_image = TRUE
        IF UPDATE(newsImage1)
            UPDATE Articles SET 
                has_image = CASE WHEN Articles.newsImage1 = '' THEN CAST(0 AS bit) ELSE CAST(1 AS bit) END
            FROM
                Articles INNER JOIN inserted ON Articles.newsid = inserted.newsid

    END
END

有谁知道如何解决这个问题?

4

0 回答 0