Re: Select with serial number column



ArunDhaJ (arundhaj@xxxxxxxxx) writes:
Help me in retrieving a table with a SNo column

Select Name From SysObjects Where type = 'U' Order By Name asc

the above query would fetch the Table names. as

Name
Table1
Table2
Table3
Table4

i need a view which should also include the serial number

SNo Name
1 Table1
2 Table2
3 Table3
4 Table4

On SQL 2005:

SELECT name, row_number() OVER(ORDER BY Name)
FROM sysobjects
ORDER BY name

On SQL 2000:

SELECT name, (SELECT COUNT(*) FROM sysobjects b
WHERE a.name <= b.name)
FROM sysobjects
ORDER BY name

On for larger data sets, the performance of the SQL 2000 solution is
so poor that it is about unusual. The alternative is to bounce data over
a temp table with an IDENTITY column.

The SQL 2005 solution has excellent performance.


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: user wants access
    ... I would strongly recommend you read Books Online for this information. ... the db_securityadmin fixed database role. ... Looking for a SQL Server replication book? ...
    (microsoft.public.sqlserver.server)
  • Re: SQL Server Management Studio cant see the 2005 engine?? But can see 2000 ?!?
    ... contains the databases that I had previously defined in SQL Server 2000. ... Installation Wizard" it says that I've got a higher version and cannot ... Just pick "Server components, tools, Books Online and samples" ...
    (comp.databases.ms-sqlserver)
  • Re: Replication
    ... The SQL CE Books Online contain a nice chart that shows you the data type ... > Server Ce database. ... > the same type as on the Sql Server Ce database? ...
    (microsoft.public.sqlserver.ce)
  • Re: Admin Version of WITH(UPDLOCK)?
    ... Right now I'd be happy with even a minimal level of concurrency from SQL ... Server via multiple JDBC connections. ... forgiving and/or better equipped to deal with deadlocks than SQL Server ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Formula Parsing
    ... > In this structure the user can add as many data fields to a customer as ... I can only echo "ZeldorBlat" don't do this in SQL. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)