December 30, 2011

Repeat rows in SQL

I have following records in my table.

image

And expected output should be:

image

T-SQL

DECLARE @Table Table
(Name VARCHAR(100))

INSERT INTO @Table
SELECT 'Jim' UNION ALL
SELECT 'Sam' UNION ALL
SELECT 'Pit' UNION ALL
SELECT 'Mac'

SELECT Name FROM @Table
CROSS JOIN
(

SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
) AS TTable(Col)

That’s It.
Enjoy Learning.

No comments:

Post a Comment