Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update multiple rows based 2 tables

Status
Not open for further replies.

soans

Technical User
Jul 21, 2000
14
0
0
US
I have three tables
Table 1
--------
key1 value
-----------
1 FALSE
2 FALSE
3 FALSE
Table 2
--------
key1 key2 value
------------------
1 A (null)
1 B (null)
1 C (null)
2 A (null)
Table 3
--------
key2 value
A 90
B 29
C 11
Using a trigger, when the value in Table 1 gets updated to TRUE, Table 2 values gets populated by Table 3 values.
IF value is TRUE for Key1=1 in Table 1 THEN Table 2 should look like
key1 key2 value
------------------
1 A 90
1 B 29
1 C 121
2 A (null)

Any solutions?
Thanks
 
For sql server

update tbl2
set value = tbl3.value
from tbl2
join inserted i
on i.key1 = tbl2.key1
join deleted d
on i.key1 = d.key1
join tbl3
on tbl2.key2 = tbl2.key2
where i.value = 'TRUE'
and d.value = 'FALSE'

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
create trigger tr1 after update on tab1
begin
update tab2 set val = (select tab3.val from tab3
where tab3.key2 = tab2.key2)
where key1 in (select key1 from tab1
where val = TRUE);
end


ANSI compliant, SQL-99 with the following non-core features:
T031, "BOOLEAN data type"
P002, "Computational completeness"
T212, "Enhanced trigger capability"
T211, "Basic trigger capability"

Note that VALUE is a reserved word, I suggest you rename these columns.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top