Solved

T-SQL Loop through recordset and UPDATE based on every record

Posted on 2004-09-02
3
1,591 Views
Last Modified: 2010-08-05
I'm brand new to T-SQL and need some help.  I'm using 2 tables.  One is called Receivers, and looks like this:

RECEIVERS
ID, [Account ID], R00
1, 0, 123
2, 0, 456
3, 0, 789

The other is called RecAccountID and looks like this:
RECACCOUNTID
ID, R00
100,123
101,456
102,789

I need to update the Receivers table [Account ID] column with the value from ID in the RecAccountID table, where they are matched on R00.  So the Receivers table needs to look like this:
RECEIVERS
ID, [Account ID], R00
1, 100, 123
2, 101, 456
3, 102, 789

Here's what I have so far:
=====================================
declare @accountID int, @recid int, @recr00 varchar(12)

declare rec_cursor CURSOR FOR
select id, r00
from receivers
where [account id] = 999999

OPEN rec_cursor

FETCH NEXT FROM rec_cursor
INTO @recid, @recr00

WHILE @@FETCH_STATUS = 0
BEGIN
      declare account_cursor FOR
      select ID
      FROM RecAccountID
      WHERE r00 = @recr00

      OPEN account_cursor

      FETCH NEXT FROM account_cursor
      INTO @accountID

      UPDATE Receivers
      SET [Account ID] = idfromabove
      WHERE ID = @accountID

      CLOSE account_cursor
      DEALLOCATE account_cursor

      FETCH NEXT FROM rec_cursor
      INTO @recid, recr00
END

CLOSE rec_cursor
DEALLOCATE rec_cursor
GO
=====================================
It's probably totally wrong.  Anyone give me some help on this?  Thanks.

Bret
0
Comment
Question by:theswally
3 Comments
 
LVL 18

Accepted Solution

by:
SjoerdVerweij earned 125 total points
Comment Utility
This should do it:

Update Receivers Set [Account ID] = ID From Receivers Inner Join RECACCOUNTID On RECACCOUNTID.R00 = Receivers.R00
0
 
LVL 2

Expert Comment

by:itstheride
Comment Utility
This should work also.

update RECEIVERS
set [Account_ID] = (select RECACCOUNTID.ID
                             from RECACCOUNTID
                             where RECACCOUNTID.R00 = RECEIVERS.R00)
0
 

Author Comment

by:theswally
Comment Utility
I tried that first, ItsTheRide, but that didn't work for me in SQL Server.

Thanks, Sjoerd!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

728 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

9 Experts available now in Live!

Get 1:1 Help Now