Solved

SQL Server Scripting - Iterate through a number of records and then alter another table

Posted on 2014-07-17
3
1,076 Views
Last Modified: 2014-07-17
Greetings,
I am working on an SQL Server script to automate a data cleansing routine as follows:
I need to select records in table ABC(20 rows returned)

For each record(20) that is returned I need to take a value from one of the columns in ABC(Ex: ABC.MY_COLUMN) and then use it to make an update in table XYZ (Ex: XYZ.MY_COLUMN).  In other words I need to take the value from ABC.MY_COULMN  and update the XYZ table with that value.

Many Thanks
0
Comment
Question by:gNome
3 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 40203296
Assuming there is a field you can join the two tables on, use UPDATE.  For example:

UPDATE XYZ
SET MY_COLUMN = a.MY_COLUMN
FROM XYZ x INNER JOIN
    ABC a ON x.ID = a.ID
WHERE a.MY_DATE >= '2014-07-01'

Open in new window

0
 
LVL 13

Accepted Solution

by:
Russell Fox earned 500 total points
ID: 40203302
The trick is that you shouldn't think about iterating over those records: that's how programmers think, not database developers. Instead of thinking about editing 20 records, think about editing one column:
UPDATE t1
SET MY_COLUMN = t2.MY_COLUMN
FROM XYZ t1
INNER JOIN (
    SELECT ID, MY_COLUMN
    FROM ABC
    WHERE StuffToGetToThose20Records
    ) t2
    ON t1.ID = t2.ID

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40203561
:) yep, change of thinking from loops/iterations to thinking joins seems needed.
tongue-in-cheek "translation" follows:

I am working on an SQL Server script
I am working on a query

to automate a data cleansing routine as follows:
to facilitate data cleansing

I need to select records in table ABC(20 rows returned)
I need a where clause or subquery to select from table ABC(n rows in resultset)

For each record(20) that is returned
For each match of these rows to another table; XYZ

I need to take a value from one of the columns in ABC(Ex: ABC.MY_COLUMN) and then use it to make an update in table XYZ (Ex: XYZ.MY_COLUMN).
I need to update a column of XYZ

In other words I need to take the value from ABC.MY_COLUMN  and update the XYZ table with that value.
In short I need an update query


In the unlikely event: no points please
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
2016 SQL Licensing 7 41
SQL Server 2012 - Merge Replication Issue 1 23
TSQL - How to declare table name 26 31
sql server tables from access 18 22
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

821 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