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 filter result set by the ROW_NUMBER function?

Execute the following SQL Server Transact-SQL script in SSMS Query Editor to filter the result set of a CTE by applying ROW_NUMBER():

USE AdventureWorks

GO

-- SQL Server common table expression - CTE - SQL Server row_number

-- SQL over order by - SQL Server row numbering - row id

DECLARE  @BeginRow INT,

         @EndRow   INT

SET @BeginRow = 200

SET @EndRow = 280;

 

WITH ctePersonContact

     AS (SELECT FirstName,

                LastName,

                Phone,

                EmailAddress,

                RowNumber = ROW_NUMBER()

                              OVER(ORDER BY FirstName, LastName)

         FROM   Person.Contact)

SELECT   [Name] = FirstName + ' ' + LastName,

         Phone,

         EmailAddress,

         RowNumber

FROM     ctePersonContact

WHERE    RowNumber >= @BeginRow

         AND RowNumber <= @EndRow

ORDER BY [Name]

 

GO

/* Partial results

 

Name                    Phone                   EmailAddress                  RowNumber

Abby Fernandez          134-555-0119            [email protected]    248

Abby Garcia             1 (11) 500 555-0154     [email protected]    249

Abby Garcia             NULL                    [email protected]   250

Abby Garcia             1 (11) 500 555-0154     [email protected]    251

Abby Garcia             NULL                    [email protected]   252

Abby Gonzalez           NULL                    [email protected]   253

*/

 

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