Author |
Topic |
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-12-15 : 20:37:35
|
I have a patient table and about 50 other tables that represent various medical forms. All have "patientname", "clientid", and "mrnum" fields. I'm a SQL novice and when I created all of this I didn't set up any relationships or foreign keys. Each table is basically on it's own.When a user updates a patient's name in my Patient table I want SQL to then go out and correct/update my other tables that contain the incorrect/old name.What is the best way to do this?Mike Brown |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-12-15 : 21:37:16
|
assuming a sensible naming convention, could use the undocumented sp_foreacthtable |
|
|
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-12-15 : 22:09:31
|
quote: Originally posted by gbritton assuming a sensible naming convention, could use the undocumented sp_foreacthtable
Thanks .... I'm a SQL newbie and after Googling "undocumented sp_foreacthtable" this is way over my head. I couldn't seem to find any clear instructions on how to do this.Mike BrownITOT Solutions, Inc.SQL Server 2012Alpha Five v3 (12) |
|
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
|
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-12-16 : 10:05:13
|
Thank you for that. Prior to me reading the forum this morning I think I found my solution in the form of a trigger. The example below updates one of my tables.USE [Home Care]GO/****** Object: Trigger [dbo].[Patient_Name_Updated] Script Date: 12/16/2014 9:01:26 AM ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[Patient_Name_Updated] ON [dbo].[Patient] AFTER UPDATEAS BEGINSET NOCOUNT ON; If (UPDATE (Firstname)) BEGIN UPDATE QANursingForms SET PatientName = Patient.Compined FROM Patient INNER JOIN QANursingForms ON QANursingForms.ClientID = Patient.ClientID AND QANursingForms.MRNum = Patient.MRNum END If (UPDATE (Lastname)) BEGIN UPDATE QANursingForms SET PatientName = Patient.Compined FROM Patient INNER JOIN QANursingForms ON QANursingForms.ClientID = Patient.ClientID AND QANursingForms.MRNum = Patient.MRNum ENDEND Mike BrownITOT Solutions, Inc.SQL Server 2012Alpha Five v3 (12) |
|
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-12-16 : 11:35:41
|
Well... yes but you said 50 tables. A trigger updating 50 other tables is not a reasonable permanent solution. |
|
|
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-12-16 : 12:10:41
|
quote: Originally posted by gbritton Well... yes but you said 50 tables. A trigger updating 50 other tables is not a reasonable permanent solution.
This is true ... I'll be taking a look at what you posted shortly.Mike BrownITOT Solutions, Inc.SQL Server 2012Alpha Five v3 (12) |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-12-16 : 13:39:13
|
You can cascade updates if the columns are in a foreign key constraint, though I wouldn't expect patientname to be in all of those tables since the name can change. I would think an identity column would be used instead, which would never get updated. I would not recommend sp_foreachtable for this nor a trigger.It seems to me that you have a design issue that needs to be fixed before proceeding.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-12-16 : 21:57:47
|
quote: Originally posted by tkizer You can cascade updates if the columns are in a foreign key constraint, though I wouldn't expect patientname to be in all of those tables since the name can change. I would think an identity column would be used instead, which would never get updated. I would not recommend sp_foreachtable for this nor a trigger.It seems to me that you have a design issue that needs to be fixed before proceeding.
Yes, there are design issues and I'm sure there are much better ways at doing all this but being a SQL noobie I didn't know any better. Would have been great to hire a consultant but couldn't find anyone under $100 an hour...too expensive. Now I have this huge database (not just the 50 tables) and things can be unwieldy at times. For the most part it all works out for my small business. I was looking more for a "quick fix".Mike BrownITOT Solutions, Inc.SQL Server 2012Alpha Five v3 (12) |
|
|
|