Skip to main content
Inspiring
October 6, 2006
Question

Looping through query to update records

  • October 6, 2006
  • 4 replies
  • 416 views
Is there a way to create some sort of a loop to update each record's last_name in QueryA with that of QueryB to produce QueryC.

I would need to update earch record:

UPDATE posnTable
SET
last_name = '#QueryB.last_name#'
WHERE title_id = #QueryA.title_id#

So, for the first record it would be:
UPDATE posnTable
SET
last_name = 'Adam'
WHERE title_id = 1111

QueryA QueryB
title_id last_name title_id last_name
1111 Zen 5555 Adam
2222 Smith 3333 Brown
3333 Brown 4444 Peterson
4444 Peterson 2222 Smith
5555 Adam 1111 Zen


QueryC
title_id last_name
1111 Adam
2222 Brown
3333 Peterson
4444 Smith
5555 Zen
    This topic has been closed for replies.

    4 replies

    Participant
    October 8, 2006
    I agree with Philh, I would not recommend relying on SQL's "natural" order since you are not guarnteed an specific order in SQL unless you use the ORDER BY clause. It may seem like SQL in inserting the fields in order now, but over time the database will insert rows where it is most efficent (which may be in the middle of the table).

    However, you can reference specific rows in a result set.

    QueryB.last_name[currentrow]
    QueryB["lastname"][currentrow]



    Inspiring
    October 8, 2006
    How do you know that the natural order of the queries would "line up" the results correctly? Is the values order simply reversed?
    jenn1Author
    Inspiring
    October 6, 2006
    Phil,
    Unfortunately that is part of the problem.
    The only thing that would equate them (although not a table field) is the #currentrow# in each query.

    Inspiring
    October 6, 2006
    How do you join query A and query B? I don't see a key that would join the two queries.

    QueryA QueryB
    title_id last_name title_id last_name
    1111 Zen 5555 Adam
    2222 Smith 3333 Brown
    3333 Brown 4444 Peterson
    4444 Peterson 2222 Smith
    5555 Adam 1111 Zen

    provides no clue as to how you would know, for example, that title_id 1111 needs to be associated with title_id 5555.