Solved

t-sql looping through rows in a table

Posted on 2006-11-07
8
4,665 Views
Last Modified: 2008-02-20
I have a need to loop through the rows of a temp table, compare certain columns, and perform an update on same columns.

I'm trying to accomplish this within a stored proc (sql server 2000).

Can someone provide sample code on how to do this or suggest a better way?

Note: I'm not to worried about efficiency at this point.

Thanks,
Rut
0
Comment
Question by:rutledgj
8 Comments
 
LVL 28

Expert Comment

by:imran_fast
ID: 17889194
Use cursors
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17889206
>Note: I'm not to worried about efficiency at this point.
note: you should ALWAYS worry about efficiency...

>I have a need to loop through the rows of a temp table, compare certain columns, and perform an update on same columns.
can you explain a bit in details?
usually, a simple UPDATE with CASE syntax can do the job.
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17889211
declare @col varchar(10),
 @co2 varchar(10)
declare c cursor for select column1, column2 from yourtable

open c
fetch next from c into into @co1 , @co2

while @@fetch_status = 0
begin
if @co1= 1
update yourtable set @col=3 where @co2= @co2
fetch next from c into into @co1 , @co2
end
close c
deallocate c
go
0
 
LVL 11

Expert Comment

by:regbes
ID: 17889226
Hi rutledgj,

do you realy need to loop through all the records this is very slow and totaly contary to set based processing

rather tell us your table structure and what you are tryong to update and we will try help you avoid cursors

rutledgj,
> Note: I'm not to worried about efficiency at this point
but soon you will be :)


HTH

R.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:srafi78
ID: 17889249
Suppose you have all the conditions linked
Update #temp set col1 = val1, col2 = val2, col3 = val3
     where col1 in (select col1 from #temp where col1 = someval1) and
               col2 in (select col1 from #temp where col2 = someval2) and
               col2 in (select col1 from #temp where col3 = someval3)

If the columns you have are independant then break up the above query as
Update #temp set col1 = val1
     where col1 in (select col1 from #temp where col1 = someval)

and repeat it for the number of columns

0
 

Author Comment

by:rutledgj
ID: 17889484
OK.
My temp table is like this:

act_id, prop_id, act_date, prop_date, act_name, prop_name, etc

In the above table, act_id WILL always = prop_id but prop_date may not be the same as act_date. So I need to update the prop_date with the act_date.


Rut
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17889504
this looks like you only needed this:

update yourtable
  set prod_date = act_date
where prod_date is null
  or prod_date <> act_date
0
 
LVL 28

Assisted Solution

by:imran_fast
imran_fast earned 125 total points
ID: 17889515
Update mytemptable set prop_Date = Act_date where act_id = prop_id
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 …
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 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

18 Experts available now in Live!

Get 1:1 Help Now