Code Snippets: Counting Records

Microsoft SQL server does not support a method of identifying the row numbers for records stored on disk, although there are a number of different techniques to associate a sequential number with a row. Today, we'll cover how you can number records by having an identity column, using a temporary table, altering tables, using Self Join, usiung a cursor, and numbering groups of records. See the full tutorial here.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 25
March 30, 2004
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Sequentially Numbering Records by Having an Identity Column


SET NOCOUNT ON
CREATE TABLE SEQ_NUMBER_EXAMPLE (
   RECORD_NUMBER INT IDENTITY 
(1,1),
   DESCRIPTION VARCHAR
(40))
INSERT INTO SEQ_NUMBER_EXAMPLE VALUES
('FIRST RECORD')
INSERT INTO SEQ_NUMBER_EXAMPLE VALUES
('SECOND RECORD')
INSERT INTO SEQ_NUMBER_EXAMPLE VALUES
('THIRD RECORD')
INSERT INTO SEQ_NUMBER_EXAMPLE VALUES
('FOURTH RECORD')
INSERT INTO SEQ_NUMBER_EXAMPLE VALUES
('FIFTH RECORD')
SELECT 
FROM SEQ_NUMBER_EXAMPLE
DROP TABLE SEQ_NUMBER_EXAMPLE


Sequentially Numbering Records by Using a Temporary Table


create table #HireDate (rank int identity, 
                    HireDate datetime,
                    LastName nvarchar(20),
                    FirstName nvarchar(20)
                    )
insert into #HireDate (HireDate, LastName, FirstName)
  select Hiredate, LastName, Firstname
    from northwind.dbo.employees
    where Title = 'Sales Representative'
    order by HireDate
Select cast(rank as char(4)) as Rank
       cast
(hiredate as varchar(23)) as HireDate,
       LastName

       FirstName from 
#HireDate
Drop table #HireDate


Sequentially Numbering Records by Altering Table


set nocount on
alter table pubs.dbo.titles
 add rownum int identity
(1,1)
go
select rownum
title from pubs.dbo.titles
   where rownum 
6
   order by rownum
go
alter table pubs
.dbo.titles
drop column rownum


Sequentially Numbering Records by Using a Self Join


SELECT count(*) RecNum,
       a
.LastName
     FROM Northwind
.dbo.Employees a join
          Northwind
.dbo.Employees b
          on a
.LastName >= b.LastName
     group by a
.LastName
     order by a
.LastName


Sequentially Number Records by Using a Cursor


declare @i int
declare 
@name varchar(200)
declare authors_cursor cursor
   
for select rtrim(au_lname) + ', ' rtrim(au_fnamefrom pubs.dbo.authors
         where au_lname 
'G' 
       order by au_lname
au_fname
open authors_cursor
fetch next from authors_cursor into 
@name
set 
@0
print 
'recnum name'
print 
'------ -------------------------------'
while 
@@fetch_status 0
begin
  set 
@= @1
  
print cast(@as char(7)) + rtrim(@name)
  fetch next from authors_cursor into 
@name
end
close authors_cursor
deallocate authors_cursor


Sequentially Numbering Groups of Records


select OD.OrderIDLineNumberOD.ProductIDUnitPriceQuantityDiscount 
  from  Northwind
.dbo.[Order DetailsOD
       join 
        
(select count(*) LineNumber
                a
.OrderIDa.ProductID
                from Northwind
.dbo.[Order DetailsA join
                     Northwind
.dbo.[Order Details
                     on  A
.ProductID >= B.ProductID
                         
and A.OrderID B.OrderID
                  group by A
.OrderIDA.ProductIDN
          on OD
.OrderIDN.OrderID and 
             OD
.ProductID N.ProductID
    where OD
.OrderID 10251
    order by OD
.OrderIDOD.ProductID

blog comments powered by Disqus
DATABASE CODE ARTICLES

- Deployment of the MobiLink Synchronization M...
- MobiLink Synchronization Wizard in SQL Anywh...
- Finding Matching Records in Data Access Pages
- Using the AccessDataSource Control in VS 2005
- A Closer Look at ADO.NET: The Command Object
- A Closer Look at ADO.NET: The Connection Obj...
- Using ADO to Communicate with the Database, ...
- Code Snippets: Counting Records
- Constraints In Microsoft SQL Server 2000
- Multilingual entries into a DB and to be dis...
- Two combos, one textbox example
- ADO Recordset Paging
- SQL Server Database Creator - .NET Version
- Getting A List of Tables From SQL Server
- Discussion & Listserv Module by Mike Eck...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials