Articlemaster テーブルがあります。ArticleMaster の Aging 列を更新したい (ArticleMaster の挿入/更新) に応じて
「NSI_Activation」テーブルの値 Month n year 列。
"skuSize" テーブルの Status の値。
条件:-
Status='R' の場合、Aging='Repeat'
そうしないと
月が 4 未満の場合、Aging=CAST(Year as varchar(4))+'1-3'
Month>4 の場合、Aging=CAST(Year as varchar(4))+'1-3'
1.私のケースのトリガーを作成する必要がありますか、それとも他の可能性はありますか?
私は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
私はたくさんグーグルで検索しましたが、道が見つかりません。トリガーにうんざり。それを行う他の可能な方法はありますか?