我有一个Articlemaster表。 我想根据
更新ArticleMaster的Aging列(适用于ArticleMaster中的插入/更新)“NSI_Activation”表格中的月份n年值列。
“skuSize”表中Status的值。
条件: -
如果Status ='R',则Aging ='Repeat'
如果Month< 4则Aging = CAST(Year as varchar(4))+'1-3'
如果Month> gt;那么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了很多但没有找到出路。 厌倦了触发器。还有其他可能的方法吗?
答案 0 :(得分: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