Solved

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

Posted on 2014-07-17
3
1,095 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 93

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 49

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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

635 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