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 2000 Forums
 Transact-SQL (2000)
 Row Count on Select

Author  Topic 

Lopaka
Starting Member

48 Posts

Posted - 2008-08-07 : 16:47:06
Is there a way to query a table and have the select do a row count automatically.

create table #temp(title nchar(1))

insert into #temp(title)
select 'a' union
select 'b' union
select 'c' union
select 'd' union

select ???, title
from #temp

--output
1 a
2 b
3 c
4 d

Thank you...:)

Robert R. Barnes

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-08-07 : 17:10:19
in 2005 this is easy with ROWNUMBER()
IN 2000

You can do a


SELECT
IDENTITY(int, 1,1) AS RowNumber
, [title]
FROM
#temp


Note I don't have a sql server at the moment so this syntax might not be 100%

-------------
Charlie
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-07 : 23:04:40
the SELECT IDENTITY required SELECT INTO . . . syntax to work


select row_no = (select count(*) from #temp x where x.title <= t.title),
title
from #temp t



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-08 : 02:51:40
quote:
Originally posted by Lopaka

Is there a way to query a table and have the select do a row count automatically.

create table #temp(title nchar(1))

insert into #temp(title)
select 'a' union
select 'b' union
select 'c' union
select 'd' union

select ???, title
from #temp

--output
1 a
2 b
3 c
4 d

Thank you...:)

Robert R. Barnes


Where do you want to show the data?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-08-08 : 04:38:02
quote:

the SELECT IDENTITY required SELECT INTO . . . syntax to work



Yes you are absolutely right. That'll teach me for posting code at home!

khtan's code is cool but it requires unique values in #temp otherwise you'll get duplicate row numbers. I think it's also quite slow on large data sets.

You could try

-- Drop the #temp table if it exists
IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp

CREATE TABLE #temp (
[title] CHAR(1)
)

INSERT INTO #temp
SELECT 'a'
UNION SELECT 'b'
UNION SELECT 'c'
UNION SELECT 'd'

-- Create a store table to keep track of row numbers
DECLARE @store TABLE (
[RowNumber] INT IDENTITY(1,1)
, [value] CHAR(1)
)

-- Insert into the @store and generate the row numbers
INSERT INTO @store
SELECT
[title]
FROM
#temp
ORDER BY
-- INSERT YOUR ORDER BY HERE
[title] ASC

--Display the results
SELECT * FROM @store


In your stored proc or whatever,

This is more complex but it will offer good performance. If you have a truly large set of data, replace the table variable with another temp table instead.

Regards,


-------------
Charlie
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-08 : 05:33:55
the best method of doing this is ofcourse Madhivanan's way. Do it in your front end application where you are displaying the data


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-08-08 : 06:27:46
quote:

the best method of doing this is ofcourse Madhivanan's way


It usually is.

-------------
Charlie
Go to Top of Page
   

- Advertisement -