Solved

cursor replacement

Posted on 2008-10-21
2
438 Views
Last Modified: 2012-05-05
Hello

I am trying follwoing code to repalace cursor, but it is giving spurious result

any suggestion/comment is welcome

Thanks
cursor.txt
0
Comment
Question by:jeet_78
2 Comments
 
LVL 10

Accepted Solution

by:
bret earned 500 total points
ID: 22768440
while exists( select * from #test where status='Y' )
begin
    set rowcount 1
   
    select @name = name,
              @key1=key1,
              @key2=key2,
              @key3=key3,
              @key4=key4
    from #test
   
    update #test
    set status='N'
   
    set rowcount 0
           
print "name: %1! , key1 : %2! , key2 : %3!, key3 : %4!, key4 : %5! ",@name,@key1,@key2,@key3,@key4    

end
-----------------

When you assign the results of a select to a variable, it is assigned the value of the
last row returned.   Your code doesn't do anything to assure that the UPDATE
is affecting the same row that was assigned to the variables.

Try changing the UPDATE to:
   update #test
    set status='N'  
    where name = @name
        and key1 = @key1
        and key2 = @key2
        and key3 = @key3
        and key4 = @key4
   

But I don't think even that is really good enough - I think you need to include indid and uid as well to form a good primary key that restricts this to one row.

This also isn't a good way to replace a cursor - you are still doing row by row processing rather than thinking about processing in terms of sets of rows.

I recommend you get Celko's new book "Thinking in Sets"  ISBN 0123741378 in particular, and his other books in general.
 
0
 

Author Closing Comment

by:jeet_78
ID: 31508308
Thank you
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

830 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