SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to apply identity for row sequencing?

Execute the following T-SQL example script in Microsoft SQL Server Management Studio Query Editor to demonstrate the use of identity function in select into temporary table.

-- SQL Server identity function  - identity( data type, starting seed, increment)

-- Select into temporary table - SQL autonumber – sequence number

USE pubs;

 

SELECT   EmployeeNumber = emp_id,

         FirstName = fname,

         MiddleInitial = minit,

         LastName = lname,

         JobNumber = IDENTITY(SMALLINT,100,1),

         JobLevel = job_lvl,

         PublicationID = pub_id,

         HireDate = convert(CHAR(10),hire_date,112)

INTO     #Employee

FROM     employee

ORDER BY LastName,

         FirstName

GO

 

SELECT *

FROM   #Employee

GO

/* Partial results

 

EmployeeNumber    FirstName   MiddleInitial     LastName    JobNumber

PMA42628M         Paolo       M                 Accorti     100

PSA89086M         Pedro       S                 Afonso      101

VPA30890F         Victoria    P                 Ashworth    102

H-B39728F         Helen                         Bennett     103

L-B31947F         Lesley                        Brown       104

F-C16315M         Francisco                     Chang       105

PTC11962M         Philip      T                 Cramer      106

A-C71970F         Aria                          Cruz        107

*/

------------

Related articles:

IDENTITY (Function) (Transact-SQL)

The behavior of the IDENTITY function when used with SELECT INTO or INSERT .. SELECT queries that contain an ORDER BY clause

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.