Teju MB
Teju MB

Reputation: 1383

Update/insert Trigger triggering the same table in SQL Server

I have a Articlemaster table . I want to update Aging column of ArticleMaster(for ever Insert/update in ArticleMaster) depending on

Conditions:-

I wrote one trigger but it was updating only 1 record but not for multiple updates.So i tried with creating with temp tables as shown below, but its going to infinite loop.

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

I googled a lot but not find the way out. Fed up with triggers. is there any other possible way to do it?

Upvotes: 0

Views: 2047

Answers (1)

Chamal
Chamal

Reputation: 1449

If i understand correctly you will be able to use something like this

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

Upvotes: 1

Related Questions