Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 each row of result set, without cursor

Author  Topic 

sqldev6363
Yak Posting Veteran

54 Posts

Posted - 2008-08-27 : 13:54:49
Hi,

can i have a query for the following scenario.
Without using cursor I need to go thru each row of my result set.

For each row in the result set i need to assign a value which i can do now with using cursor, but without using cursor i want to do.

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-27 : 14:00:29
There is no point in not using a cursor if you are going to process every row in a table. That's exactly what a cursor is intended for.

You could switch to a WHILE loop, however there are no benefits over a cursor.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-27 : 14:24:17
quote:
Originally posted by sqldev6363

Hi,

can i have a query for the following scenario.
Without using cursor I need to go thru each row of my result set.

For each row in the result set i need to assign a value which i can do now with using cursor, but without using cursor i want to do.

Thanks



That depends on your exact scenario. There are case where you could go for set based value assignement but in some cases row by row value manipulation needs to be performed. The former can be dealt with set based technique. Can you provide more info on what you're trying to do so that we can clearly suggest whether there's any oither way?
Go to Top of Page

sqldev6363
Yak Posting Veteran

54 Posts

Posted - 2008-08-27 : 14:29:32
TableA

Col1 Col2 Col3 Col4 Col5
AAA BBB CCC DDD 9999
AAA BBB CCC DDD 9999
AAA BBB CCC DDD 9999
www xxx yyy zzz 9999
www xxx yyy zzz 9999
www xxx yyy zzz 9999
PPP QQQ RRR SSS 9999
PPP QQQ RRR SSS 9999
PPP QQQ RRR SSS 9999
PPP QQQ RRR SSS 9999


In the above table i have 10 rows. In that first 3 rows, next 3 rows and next 4 rows contains same values.

So,what i need is, in col5 i have 9999 for all the rows. I dont want 9999 for all columns, i need the incremental value for the matched rows in col5, mean like i need value 1 instead of 9999 for the 1st 3 rows and for the next 3 rows value 2 and the for next 4 rows i need value 3...... Like that i need increment value by writing a query.

I need query without using Cursors
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-27 : 14:33:43
Duplicate, locking thread.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -