User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 373,932 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,207 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 952 | Replies: 3
Reply
Join Date: Jan 2008
Location: India
Posts: 141
Reputation: sbv is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
sbv's Avatar
sbv sbv is offline Offline
Junior Poster

can we get the position of record while retriving records in sql server 2003

  #1  
Mar 25th, 2008
hi
i am using sql server 2003. can we get the position of record / row index in sql server while retrieving records?

like in some case we fetch records as per our criteria. And if that time need to know the postion of particular record then? how to get that?

plz help.
Accept Challenges and Enjoy Coding... :)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Posts: 68
Reputation: cmhampton is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: can we get the position of record while retriving records in sql server 2003

  #2  
Mar 25th, 2008
Are you using a cursor?

If so, you could always create a counter variable and increment it each time through the loop. Is this done through an application, or the query window? If it's done in a application, you may need to create a temporary table, insert the counter, plus whatever info you want, then select all the records from it after you are done with the fetch loop. If you are running it in a query window, you can just print the output.

Here's an example using the temp table
  1. DECLARE @counter int;
  2. @counter = 0;
  3.  
  4. CREATE TABLE #temp (counter int, otherdata varchar(255)) --Use whatever columns you need.
  5.  
  6. DECLARE tempCursor CURSOR FOR SELECT * FROM TABLE
  7. OPEN tempCursor
  8.  
  9. DECLARE @value AS int --or whatever your datatype is
  10.  
  11. FETCH NEXT FROM tempCursor INTO @value
  12.  
  13. WHILE @@FETCH_STATUS = 0
  14. BEGIN
  15.  
  16. INSERT #temp (counter, otherdata) VALUES (@counter, 'otherdata')
  17. @counter = @counter+1
  18.  
  19. FETCH NEXT FROM tempCursor INTO @value
  20. END
  21.  
  22. CLOSE tempCursor
  23. DEALLOCATE tempCursor
  24.  
  25. SELECT * FROM #temp
  26.  
  27. DROP TABLE #temp

It's probably not the best solution, but it would work.
Reply With Quote  
Join Date: Oct 2005
Location: Manchester, UK
Posts: 481
Reputation: pty is on a distinguished road 
Rep Power: 3
Solved Threads: 30
pty's Avatar
pty pty is offline Offline
Posting Pro in Training

Re: can we get the position of record while retriving records in sql server 2003

  #3  
Mar 25th, 2008
Originally Posted by sbv View Post
hi
i am using sql server 2003. can we get the position of record / row index in sql server while retrieving records?

like in some case we fetch records as per our criteria. And if that time need to know the postion of particular record then? how to get that?

plz help.


A quick and dirty method would be to select into a temp table that has a identity column and then use that as the basis for your query. I wouldn't use this method if performance is high on your agenda
Note to self... pocket cup
Reply With Quote  
Join Date: Jan 2008
Location: India
Posts: 141
Reputation: sbv is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
sbv's Avatar
sbv sbv is offline Offline
Junior Poster

Re: can we get the position of record while retriving records in sql server 2003

  #4  
Mar 26th, 2008
hi

thank you very much for your reply. But i dont want to run a loop for such thing and use a variable.
and as pty says using a identity column i.e a pk what if i select records as per my criteria.
Accept Challenges and Enjoy Coding... :)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 6:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC