Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old February 13th, 2005, 10:42 AM
rangers rangers is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 20 rangers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 54 m 46 sec
Reputation Power: 0
create running counter for SQL data (select)

for eg i got previous data such as
customer name
---------
jim
kim
roy

how do i create counter in the select statement, so that the
result appears as
no customer name
---------------
1 jim
2 kim
3 roy

Reply With Quote
  #2  
Old February 13th, 2005, 04:11 PM
A2k's Avatar
A2k A2k is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 166 A2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 44 m 10 sec
Reputation Power: 4
Ok, here is some code that will achieve the desired result, however I am convinced that there is an easier way.

Code:
declare @Count numeric

create table #tmpResultSet (
	field1 numeric,
	field2 nvarchar(255))

insert into #tmpResultSet
select '', [name]
from someTable

declare csrNameSet as cursor for
select field1 from #tmpResultSet

select @count=1

open csrNameSet

while @@FETCH_STATUS <> 0
begin
	fetch next from csrNameSet into
	@name
	update #tmpResultSet set field1=@count
	where #tmpResultSet.Field2=@name
	select @count=@count+1
end

close csrNameSet
deallocate csrNameSet

select * from #tmpResultSet

drop table #tmpResultSet


you might try turning field1 from #tmpResultSet into an ID field, that will count up with what ever increment you like. That will cut the code down to about 10 lines.

Reply With Quote
  #3  
Old February 13th, 2005, 11:25 PM
Tonny-Soeroso Tonny-Soeroso is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 36 Tonny-Soeroso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 56 m 27 sec
Reputation Power: 0
Thumbs up

The simple one :


use yourDB
go

declare @temp table (id int identity(1,1) , name char(100))

insert @temp(name)
select name from tblCustomerName


select * from @temp

The idea of this solution is to create temporary table with 2 fields, one field is identity field seed (1,1)
another field is containing name from customer table.. i think this solution more simple and quick to load because
it doesn't need any cursor that fetch each record.


Regards,


Tonny Soeroso

Reply With Quote
  #4  
Old February 14th, 2005, 06:33 AM
A2k's Avatar
A2k A2k is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 166 A2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 44 m 10 sec
Reputation Power: 4
precisely... Exactly what I thouhgt, after making an over complex way of doing it!

Reply With Quote
  #5  
Old February 15th, 2005, 02:23 AM
rangers rangers is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 20 rangers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 54 m 46 sec
Reputation Power: 0
thanks alot, it's what i need

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > create running counter for SQL data (select)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT