Solved

Help with SQL to change data in multiple tables

Posted on 2014-04-02
3
423 Views
Last Modified: 2014-04-02
I have a job table with a "jobID" as primary key.   I have multiple related tables that have a "jobID" foreign key column.  I need to correct bad "jobID" data across all the tables.   The name "jobID" is used in all the tables.  

This query (found online) gives me all the tables that contain the "jobID" column.

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%jobID%'
ORDER BY schema_name, table_name;
0
Comment
Question by:HLRosenberger
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
E.g.

WITH    AffectedTables
          AS ( SELECT   QUOTENAME(S.name) + '.' + QUOTENAME(T.name) AS table_name ,
                        QUOTENAME(c.name) AS column_name
               FROM     sys.columns C
                        INNER JOIN sys.tables T ON C.object_id = T.object_id
                        INNER JOIN sys.schemas S ON T.schema_id = S.schema_id
               WHERE    C.name LIKE '%ID' -- %jobID%'
             )
    SELECT  'UPDATE ' + table_name + ' SET ' + column_name + ' = 123 WHERE ' + column_name + ' = 666;' AS update_statement
    FROM    AffectedTables
    ORDER BY table_name;

Open in new window


Caveat: Make a backup first.

As foreign key constraints and triggers may be a problem you should test your entire update script by enclosing it into explicit transaction control with TRY-CATCH.
0
 
LVL 1

Author Comment

by:HLRosenberger
Comment Utility
thanks.
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
Comment Utility
thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now