Solved

t-sql looping through rows in a table

Posted on 2006-11-07
8
4,681 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
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.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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 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…

697 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