0

我有一个 Articlemaster 表。我想更新 ArticleMaster 的老化列(永远在 ArticleMaster 中插入/更新),具体取决于

  • “NSI_Activation”表中的值 Month n year 列。

  • “skuSize”表中 Status 的值。

条件:-

  • 如果 Status='R' 那么 Aging='Repeat'

    别的

  • 如果 Month <4 那么 Aging=CAST(Year as varchar(4))+'1-3'

  • 如果 Month>4 则 Aging=CAST(Year as varchar(4))+'1-3'

    1.是否需要为我的案例创建触发器或是否存在任何其他可能性?

我写了一个触发器,但它只更新了 1 条记录,但没有更新多个记录。所以我尝试使用如下所示的临时表创建但它进入了无限循环。

ALTER TRIGGER AgingUpdation
   ON  Articlemaster
   AFTER INSERT,UPDATE
AS 
BEGIN
IF ((
SELECT TRIGGER_NESTLEVEL()) > 1 )
RETURN

    SET NOCOUNT ON;
    DECLARE @i INT, @d INT,@c INT,@month INT;
    DECLARE @MonthTable TABLE(idx bigint Primary key identity(1,1),ArticleCode varchar(30),CompCode varchar(20),MonthNo INT,DateToMarketYear INT,[Status] char(1))
    SELECT @i = COUNT(*) FROM inserted;
    SELECT @d = COUNT(*) FROM deleted;
    insert into  @monthtable(ArticleCode,CompCode,MonthNo,DateToMarketYear,[Status]) 
    Select  I.ArticleCode,I.CompCode,N.DatetoMarketMonth,N.DateToMarketYear,(select top 1 Status from SkuSize S  INNER JOIN Inserted I on S.ArticleCode=I.ArticleCode and S.CompCode=I.CompCode)
     from NSI_Activation N inner join Inserted I on N.ArticleCode=I.ArticleCode and I.Compcode=N.CompCode 
    IF @i + @d > 0
    BEGIN
        IF @i > 0 AND @d = 0    -- Insert
          BEGIN     
                Set @c=1
                While(@c<=(SELECT MAX(idx) from @MonthTable))
                BEGIN
                    if (Select  [Status] from  @MonthTable where idx=@c)='R' 
                    update A set A.Aging='Repeat' from ArticleMaster A 
                    inner join @MonthTable T on A.ArticleCode=T.ArticleCode
                    else If (Select  MonthNo from @MonthTable where idx=@c)<4
                    update A set A.Aging=CAST(T.DateToMarketYear as varchar(4))+'_1-3' from ArticleMaster A inner join @MonthTable T  on A.ArticleCode=T.ArticleCode and A.Compcode=T.CompCode 
                    else
                    update A set A.Aging=CAST(T.DateToMarketYear as varchar(4))+'_4-12' from ArticleMaster A inner join @MonthTable T  on A.ArticleCode=T.ArticleCode and A.Compcode=T.CompCode 
                END
                set @c=@c+1
           END

        IF @i > 0 AND @d > 0  --Update
            BEGIN
                Set @c=1
                While(@c<=(SELECT MAX(idx) from @MonthTable))
                BEGIN
                    if (Select  [Status] from  @MonthTable where idx=@c)='R' 
                    update A set A.Aging='Repeat' from ArticleMaster A 
                    inner join @MonthTable T on A.ArticleCode=T.ArticleCode
                    else If (Select  MonthNo from @MonthTable where idx=@c)<4
                    update A set A.Aging=CAST(T.DateToMarketYear as varchar(4))+'_1-3' from ArticleMaster A inner join @MonthTable T  on A.ArticleCode=T.ArticleCode and A.Compcode=T.CompCode 
                    else
                    update A set A.Aging=CAST(T.DateToMarketYear as varchar(4))+'_4-12' from ArticleMaster A inner join @MonthTable T  on A.ArticleCode=T.ArticleCode and A.Compcode=T.CompCode 
                END
                set @c=@c+1
            END
    END    

END

我google了很多,但没有找到出路。厌倦了触发器。还有其他可能的方法吗?

4

1 回答 1

1

如果我理解正确,您将能够使用这样的东西

UPDATE A
    SET A.Aging=(CASE 
                   WHEN s.Status='R' THEN 'Repeat'
                   WHEN s.Status!='R' and N.MonthNo <4 THEN CAST(N.DateToMarketYear as varchar(4))+'_1-3'
                   WHEN s.Status!='R' and N.MonthNo >=4 THEN CAST(N.DateToMarketYear as varchar(4))+'_4-12'
                   ELSE A.Aging
                 END)
    FROM ArticleMaster A 
    inner join inserted I ON A.ArticleCode=I.ArticleCode and A.Compcode=I.CompCode 
    inner join NSI_Activation N ON N.ArticleCode=I.ArticleCode and N.Compcode=I.CompCode 
    inner join SkuSize S ON S.ArticleCode=I.ArticleCode and S.Compcode=I.CompCode
于 2013-09-13T12:09:35.820 回答