943,655 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 4141
  • MS SQL RSS
Mar 25th, 2008
0

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

Expand 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.
sbv
Reputation Points: 15
Solved Threads: 8
Junior Poster
sbv is offline Offline
178 posts
since Jan 2008
Mar 25th, 2008
1

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

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
SQL Syntax (Toggle Plain Text)
  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.
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008
Mar 25th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by sbv ...
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
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Mar 26th, 2008
0

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

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.
sbv
Reputation Points: 15
Solved Threads: 8
Junior Poster
sbv is offline Offline
178 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: looping in SQL Server2005
Next Thread in MS SQL Forum Timeline: Urgent - how to calculate rank in selected records in sql server 2000





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC