December 31, 2011

Add serial number in select result SQL

We have following select result records.

image

And my desired output should be:

image

T-SQL

DECLARE @Table Table
(SQLForum VARCHAR(100))

INSERT INTO @Table
SELECT 'MSDN' UNION ALL
SELECT 'StackOverFlow' UNION ALL
SELECT 'SQLServerCentral' UNION ALL
SELECT 'SQLTeam'

SELECT ROW_NUMBER()OVER(ORDER BY(SELECT 1))AS 'SRNo',* FROM @Table


That’s It.
Enjoy Learning.

No comments:

Post a Comment