DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to generate sequence in SQL Server 2005?

Execute the following SQL Server T-SQL scripts in Management Studio Query Editor to demonstrate integer sequence generation.

USE AdventureWorks

 

-- Small sequence generator - SQL row_number over order by

SELECT TOP 100 SeqNo = row_number()

                         OVER(ORDER BY object_id ASC)

FROM   sys.objects ORDER BY SeqNo

 

GO

 

/* Partial results

 

SeqNo

1

2

3

4

5

6

7

8

9

10

*/

 

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

-- SQL sequence generator recursive CTE

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

-- SQL recursive cte - Common Table Expression

-- SQL maxrecursion option - SQL integer sequence

WITH cteIntegerSequence(SeqNo)

     AS (SELECT 1

         UNION ALL

         SELECT SeqNo + 1

         FROM   cteIntegerSequence

         WHERE  SeqNo < 1000)

SELECT TOP 100 *

FROM   cteIntegerSequence

ORDER BY SeqNo

OPTION (MAXRECURSION 0)

GO

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE