Solved

t-sql looping through rows in a table

Posted on 2006-11-07
8
4,669 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

910 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

22 Experts available now in Live!

Get 1:1 Help Now