MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - Sequential Numbering and Counting of Recor...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

Sequential Numbering and Counting of Records
By: Gregory A. Larsen
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2004-03-29

    Table of Contents:
  • Sequential Numbering and Counting of Records
  • Sequentially Numbering Records by Using a Temporary Table
  • Sequentially Numbering Records by Altering Table
  • Sequentially Numbering Records by Using a Self Join
  • Sequentially Number Records by Using a Cursor
  • Sequentially Numbering Groups of Records

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Sequential Numbering and Counting of Records - Sequentially Numbering Records by Using a Temporary Table


    (Page 2 of 6 )

    Now you might not have designed your table to have an identity column, or even want to place one on your existing table, so another option is to insert the records you desired to have a sequence number into a temporary table. Here is some code that takes the Northwind.dbo.Employees table and copies only the Sales Representatives into a temporary table. This example uses this temporary table with a rank identity column to show a ranking of Sales Representatives by HireDate.


    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

    The output of this example looks like this:


    Rank HireDate                LastName             FirstName            
    ----------------------------------------------------- 
    1    Apr  1 1992 12
    :00AM     Leverling            Janet
    2    May  1 1992 12
    :00AM     Davolio              Nancy
    3    May  3 1993 12
    :00AM     Peacock              Margaret
    4    Oct 17 1993 12
    :00AM     Suyama               Michael
    5    Jan  2 1994 12
    :00AM     King                 Robert
    6    Nov 15 1994 12
    :00AM     Dodsworth            Anne

    More MS SQL Server Articles
    More By Gregory A. Larsen


     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





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