ROW_NUMBER() Example

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for programmers

-- Declare Variables
DECLARE	@SourceTable TABLE (Name VARCHAR(50))
DECLARE	@DestinationTable TABLE (MyID INT, Name VARCHAR(50))

-- Setup Test Data
INSERT INTO @SourceTable
SELECT 'Matthew'
INSERT INTO @SourceTable
SELECT 'Mark'
INSERT INTO @SourceTable
SELECT 'Luke'
INSERT INTO @SourceTable
SELECT 'John'

-- Insert Data
INSERT INTO @DestinationTable
SELECT	ROW_NUMBER() OVER ( ORDER BY Name ), Name FROM @SourceTable

-- Select Results
SELECT * FROM @DestinationTable

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results
:records returned in :time ms:cached