Solved

t-sql looping through rows in a table

Posted on 2006-11-07
8
4,690 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 28

Expert Comment

by:imran_fast
ID: 17889194
Use cursors
0
 
LVL 143

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 143

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

631 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