Mozart Al Khateeb

Full Stack

Mobile

.Net Developer

ERP Developer

Mozart Al Khateeb

Full Stack

Mobile

.Net Developer

ERP Developer

Blog Post

Sql Server, Duplicating And Manipulating Rows Without Loops

March 17, 2019 Database
Sql Server, Duplicating And Manipulating Rows Without Loops

This example shows how we can use “CROSS APPLY” in SQL Server To duplicate and manipulate rows without the need of a loop or cursor:

SELECT a, b, c INTO #tmp
FROM
(SELECT 1 a, 2 b, 10 c
UNION ALL
SELECT 2 a, 5 b, 10 c) tb;

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

SELECT * FROM #tmp;

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

SELECT C.* 
FROM #tmp tb1
CROSS APPLY (VALUES(tb1.a, tb1.b, tb1.c), (tb1.a *2, tb1.b *5, tb1.c)) C(a, b, c);
Taggs:
Write a comment