Hi I have a table with data like this...
A B C D E
100 A1 Hard Dece 100
100 A1 Hard Jan 200
100 A1 SOFT Jan 200
200 A2 Hard Dec 250
200 A2 SOFT Jan 300
----------------------------
Then when an insert or update performed on this table i need to execute a trigger and The data should be like below
A B C F
100 A1 Hard Dece:100;Jan:200
100 A1 SOFT Jan:200
200 A2 Hard Dec:250
------------------------
I have created a trigger like below
ALTER TRIGGER [dbo].[trgInsert9] ON [roll].[dbo].[testing]
after INSERT,Update
AS
Declare @Counter int=0
Declare @max int
Declare @re int
select * into #t13 from (select A,B,C,[D] + ':' + cast([E] as varchar) as Common from [roll].[dbo].[testing]) as X
select * into #t24 from (
SELECT t2.A,t2.B,t2.C,
STUFF((SELECT ';' + CAST(Common AS varchar)
FROM #t13 t1 where t1.A =t2.A and t1.B=t2.B and t1.C=t2.C
FOR XML PATH('')), 1 ,1, '') AS List
FROM #t13 t2
GROUP BY t2.A,t2.B,t2.C) as XT
SET @max=(select count(A) from #t24)
while(@Counter<@max)
BEGIN
if exits(select count(t11.A) from Test123 t11 left join #t24 f on t11.A=f.A
where t11.A=f.A and t11.B=f.B and t11.C=f.C
and t11.ValueList=f.ValueList)
Begin
update Test123 set Test123.ValueList=t4.ValueList from #t24 t4
where
Test123.A=t4.A and Test123.B=t4.B and Test123.C=t4.C
END
ELSE
BEGIN
insert into Test123
select * from #24 t8
END
SET @Counter=@Counter+1
END
drop table #t13
drop table #t24
But i am not getting proper result like if the the first is inserted and then it is not updating means it is adding that to the next one like below:-
A B C F
100 A1 Hard Dece:100
100 A1 Hard Dece:100;jan:200
------------------------------
But in temp table i am getting proper result, when i tried to insert or update then the problem is occuring please help me.