Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help in a trigger !

Author  Topic 

sqlilliterate
Starting Member

40 Posts

Posted - 2008-08-21 : 07:32:49
I’ve 2 tables, tr_test1 & tr_test2 with same schema. Also the data is same in both the tables.
I wrote a trigger, which will delete a row from the table tr_test2, when a corresponding row is updated in tr_test1 table.

create table tr_test1 (id int,name varchar(100)) 

insert into tr_test1
select 1,'name1' union all
select 2,'name2' union all
select 3,'name3' union all
select 4,'name4'

select * into tr_test2 from tr_test1

CREATE TRIGGER tr_test
ON tr_test1
FOR update AS
DELETE FROM tr_test2 where id in (select id from deleted)

update tr_test1 set id = 5 where id = 4

It works fine, when I execute the query like update tr_test1 set id = …. , and the corresponding row is deleted from the tr_test2 table.

But, in object Explorer – Right click table tr_test1 – open table, when I try modify a value, it throws me the following error…

No row was updated.

The data in row 3 was not committed.
Error source: Microsoft.VisualStudio.DataTools.
Error Message: The tor value(s) updated or deleted either do not make the row unique or they alter multiply rows(2 rows)

Correct the errors and retry or press esc to cancel the change(s).

Any thoughts on ths would immensely help me…

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-21 : 08:18:09
Do it as update statement in a query analyser
Dont do this by opening a table


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sqlilliterate
Starting Member

40 Posts

Posted - 2008-08-21 : 12:02:13
No madhi...
I'm afraid all my users will execute the query...
most of them are used to change the data manually thru the GUI :(
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-22 : 03:30:17
quote:
Originally posted by sqlilliterate

No madhi...
I'm afraid all my users will execute the query...
most of them are used to change the data manually thru the GUI :(


That is not the effecient method and you will get errors if there are more than a row for a key

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -