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 2012 Forums
 Transact-SQL (2012)
 Table(s) update help

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
Go to Top of Page

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 Brown
ITOT Solutions, Inc.
SQL Server 2012
Alpha Five v3 (12)
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-16 : 09:08:18
This link: http://www.databasejournal.com/features/mssql/article.php/3441031/SQL-Server-Undocumented-Stored-Procedures-spMSforeachtable-and-spMSforeachdb.htm

is a pretty good description of how to do this kind of thing. First it shows you a cursor-based approach then a second method using the undocumented stored procedure. Work through it slowly and understand each piece. then, give it a try in your development sandbox. It's worth learning.
Go to Top of Page

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 ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[Patient_Name_Updated]
ON [dbo].[Patient]
AFTER UPDATE

AS
BEGIN
SET 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
END
END


Mike Brown
ITOT Solutions, Inc.
SQL Server 2012
Alpha Five v3 (12)
Go to Top of Page

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.
Go to Top of Page

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 Brown
ITOT Solutions, Inc.
SQL Server 2012
Alpha Five v3 (12)
Go to Top of Page

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 Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

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 Brown
ITOT Solutions, Inc.
SQL Server 2012
Alpha Five v3 (12)
Go to Top of Page
   

- Advertisement -