Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Create Trigger
Message
 
 
À
07/10/2009 01:09:19
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Titre:
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Desktop
Divers
Thread ID:
01427973
Message ID:
01427984
Vues:
80
This message has been marked as the solution to the initial question of the thread.
Ok, I solved it - but it was a bit tough
BEGIN TRAN

create table Test (ID varchar(1), Name varchar(10), FollowID varchar(1) null)
insert into Test values ('a','First','b'), ('a', 'Second', 'e'), 
('b', 'Third', 'e'), ('c','4th','e'), ('d','d','e'), ('e','e',null) 
go
create trigger tr_Test_Update on Test for update as
begin
	update T set FollowID = I.ID from Test T 
	inner join Deleted Del on T.FollowID =Del.ID 
	cross join Inserted I 
end
go
select * from Test
update Test set ID = 'f' where ID = 'e'
select * from Test
ROLLBACK
The main thing was figuring that we need to use a cross join with Inserted since there is no actual link anymore...

I'm thinking, it would not work if we can try

update ID = ID + 'aa' where ID = 'bb'

>not working correctly
>
>let say TableA have this row
>
>id name followid
>--- --------- -----------
>a a e
>b b e
>c c e
>d d e
>e e null
>
>when changing e to f
>
>id name followid
>--- --------- -----------
>a a f
>b b f
>c c f
>d d f
>f e null
>
>
>
>
>>>Can someone help me with this :-
>>>
>>>Table A
>>>-----------
>>>id
>>>name
>>>followid (follow another record id)
>>>
>>>
>>>i need to create a trigger on Table A so whenever the id change,
>>>a trigger will fire on Table A to update all the followid that have the same value as the inserted.id
>>>
>>>
>>>thanks in advance
>>
>>You need to be careful to not fall into infinite loop
>>
>>create trigger tr_TableA_Update ON TableA for UPDATE
>>AS
>>  BEGIN
>>         update TableA set FollowID = Inserted.ID from TableA 
>>inner join Inserted on ID = Inserted.ID inner join Deleted on TableA.FollowID = Deleted.ID
>> END
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform