473,382 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Loop Statement to compare two tables

I am trying to run a loop statement that compares two tables. We will call them TableA & TableB and pks of LastNameA (tableA) and LastNameB (tableB). TableA is a table that was created some time ago. I want to do a loop that compares the data in TableA to TableB, if it finds that the date for tableA is longer ago than the date for TableB, I want it to update the day for TableA to the date in tableB. Also if the loop finds that there is no information for a last name in TableA as there is in TableB I want it to update TableA to hold the new LastName & date from tableB.



Example:

Table A
LastName (pk) Date
Johnson 5/6/2007
Ralph 9/10/2006
Billings 11/12/2000

Table B
LastName (pk) Date
Johnson 7/7/2007
Ralph 10/11/2005
Billings 12/31/2006
Simmons 7/26/2007

New TableA
LastName (pk) Date
Johnson 7/7/2007
Ralph 9/10/2006
Billings 12/31/2006
Simmons 7/26/2007
Jul 26 '07 #1
2 2384
ilearneditonline
130 Expert 100+
You don't really need to `loop` through the records. You can do a simple update. If you could have new records, then you could so something similar with an insert. But you only mentioned updating the existing records, so that is what I went with.

This should update the records where the date on the comparison table is greater than the current table.

Expand|Select|Wrap|Line Numbers
  1. UDPATE TableA
  2.     SET thedate=b.thedate
  3. FROM TableB b INNER JOIN TableA a
  4. ON a.LastName=b.LastName
  5. WHERE b.thedate > a.thedate
  6. GO
  7.  
  8. UDPATE TableB
  9.     SET thedate=a.thedate
  10. FROM TableB b INNER JOIN TableA a
  11. ON a.LastName=b.LastName
  12. WHERE a.thedate > b.thedate
  13. GO
Jul 28 '07 #2
ilearneditonline
130 Expert 100+
And you could try something like this for new records.

Expand|Select|Wrap|Line Numbers
  1. INSERT TableA(LastName, thedate)
  2. SELECT b.LastName, b.thedate
  3. FROM TableB b LEFT OUTER JOIN TableA
  4. ON b.LastName=a.LastName
  5. WHERE a.LastName ISNULL
Jul 28 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Michael | last post by:
I have two tables with a 1-many relationship. I want to write a select statement that looks in the table w/many records and compares it to the records in the primary table to see if there are any...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
10
by: rcamarda | last post by:
I have created a table that contains buckets to hold activitives of enrollment for each of our admissions officer for each day of an enrollment session. I have an UPDATE that builds rolling totals...
16
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataAdapter. For every record in tblA where colB = 'abc', I want to update the value in colA. In VB6, using ADO I can loop thru the recordset,set the...
1
by: Perl Beginner | last post by:
I hope i can articulate this question properly. i have been trying to figure this out for over a week. I am comparing the contents of two files, but the comparison is done inside of a foreach...
4
by: gillianbrooks91 | last post by:
Forgive me for asking this question, I've trawled through nearly every available post on this subject that I can find for a few weeks now but nothing quite points me in the right direction. I'm...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.