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 |
|
mohanjohn
Starting Member
3 Posts |
Posted - 2008-01-16 : 09:44:02
|
| Persons TablePersonID int NOT NULL PRIMARY KEY,PersonFatherID int NULL FOREIGN KEY Persons(PersonID)PersonMotherID int NULL FOREIGN KEY Persons(PersonID)PersonGeneration int NULLPersonFirstName nchar(20) NOT NULLPersonLastName nchar(20) NOT NULLSpouses TableSpouseID int NOT NULL PRIMARY KEYHusbandID int NOT NULL FOREIGN KEY Persons(PersonID)WifeID int NOT NULL FOREIGN KEY Persons(PersonID)Persons Table DataPersonID PersonFatherID PersonMotherID PersonGeneration PersonFirstName PersonLastName1 1 1 1 Adam Smith2 2 2 1 Evelyn Smith3 1 2 2 Caleb Smith4 NULL NULL 0 Sara JonesSpouses Table DataSpouseID HusbandID WifeID1 1 22 3 4I want to update PersonGeneration column in Persons table when that person is added into Spouses table. For example, if Sara Jones is added as wife to Caleb Smith in Spouses table, then it should update Sara's PersonGeneration (0) with Caleb's PersonGeneration (2) in Persons table. I am thinking about a trigger or a stored procedure or both. Any help would be appreciated.Thanks,Mohan John |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-16 : 10:12:06
|
You can use trigger like thisCREATE TRIGGER MyTrig ON SpousesAFTER INSERTASUPDATE pSET p.PersonGeneration =p2.PersonGenerationFROM Persons pINNER JOIN INSERTED iON i.WifeID=p.PersonIDINNER JOIN Persons p2ON p2.PersonID=i.HusbandIDGO |
 |
|
|
mohanjohn
Starting Member
3 Posts |
Posted - 2008-01-16 : 11:15:03
|
quote: Originally posted by visakh16 You can use trigger like thisCREATE TRIGGER MyTrig ON SpousesAFTER INSERTASUPDATE pSET p.PersonGeneration =p2.PersonGenerationFROM Persons pINNER JOIN INSERTED iON i.WifeID=p.PersonIDINNER JOIN Persons p2ON p2.PersonID=i.HusbandIDGO
Thanks, I will try this. |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-01-16 : 22:52:50
|
| Personally I would lock down direct access to the tables and use a SP for all operations. Triggers cause all sorts of side effects (as programmed of course) that are not always obvious or desireable. Having an API approach is more clear. You will get arguments both ways, but I find the unexpected side effects of triggers can cause more confusion than a clearly defined API. |
 |
|
|
|
|
|