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.
| Author |
Topic |
|
jose1lm
Yak Posting Veteran
70 Posts |
Posted - 2004-07-30 : 19:31:40
|
| I'm not really sure how to go about doing this but if someone can help me out or point me in right direction.I would like the column 'rev' in the table 'iso_employwi' to automatically be populated by referencing the column 'rev' in the table 'iso_wi'. So in other words, when an insert is done on table 'iso_employwi', it fills the 'rev' column by matching the 'wi' column from the table 'iso_wi'.Table: iso_wiPrimary Key: wiwi | wi_title | revTable: iso_employwiPrimary key: empWiDempWiD | pin | wi | revThe table 'iso_employwi' would look like this if it can be done:empWiD | pin | wi | rev001 | 1 | WI001 | B002 | 1 | WI002 | A003 | 2 | WI001 | Betcusing SQL2000JLM |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-07-31 : 02:06:01
|
You could create an INSERT & UPDATE trigger on iso_employwi that performs an UPDATE on iso_wiSomething like:CREATE TRIGGER dbo.iso_employwi_TRON dbo.iso_employwiAFTER INSERT, UPDATEASSET NOCOUNT ON UPDATE iso_wi SET rev = iso_wi.rev FROM inserted I JOIN dbo.iso_wi ON iso_wi.wi = I.wiGO Kristen |
 |
|
|
jose1lm
Yak Posting Veteran
70 Posts |
Posted - 2004-08-02 : 12:43:21
|
| Thanks Kristen,I am not getting the results I was looking for. Maybe I didn't explain myself too good. =(After I insert or update a specific employee record, the trigger is getting the correct value from 'iso_wi.rev', but it's updating all the rows in the column 'iso_employwi.rev' with that data even if 'iso_wi.wi' are not equal. I only want to update the record(s) where '.wi' matches from both tables.Here is the results on what it's doing:empWiD | pin | wi | rev001 | 1 | WI001 | B002 | 1 | WI002 | B003 | 2 | WI001 | B004 | 2 | WI034 | B005 | 2 | WI120 | B006 | 3 | WI002 | BAnd it should look like this:001 | 1 | WI001 | B002 | 1 | WI002 | A003 | 2 | WI001 | B004 | 2 | WI034 | D005 | 2 | WI120 | H006 | 3 | WI002 | AAn example of what the 'iso_wi' table looks like:wi | wi_title | revWI001 | Test A | BWI002 | Test B | AWI001 | Test C | BWI034 | Test D | DWI120 | Test E | HWI002 | Test F | AAlso, could you help me with another trigger (or would it be setting up a relationship?). When an update is done in 'iso_wi', I would like it to also update all the matching data in the table 'iso_employwi'. So if the columns 'wi or rev' in 'iso_wi' get updated, it should also make the same change in 'iso_employwi'.JLMUpdate:I setup a relationship between both tables but I can't figure out how to make it work for two sets of columns. I only have it working for the 'wi' columns bewteen both tables.******ALTER TABLE [dbo].[iso_employwi] ADD CONSTRAINT [FK_iso_employwi_iso_wi] FOREIGN KEY ( [wi] ) REFERENCES [dbo].[iso_wi] ( [wi] ) ON DELETE CASCADE ON UPDATE CASCADE******So if I delete a row, or update a field in the column 'wi' in iso_wi, then the other table 'iso_employwi' also gets update which is what I want. If I can only get it to also do the 'rev' area then maybe I don't need triggers, right? I'm just not sure how to go about doing this. =( |
 |
|
|
jose1lm
Yak Posting Veteran
70 Posts |
Posted - 2004-08-05 : 13:16:40
|
| Can anyone else help me figure this out. I'm still trying different things but they just don't seem to work. :/ |
 |
|
|
|
|
|
|
|