SQL Server / T-SQL :- Based on the Column value duplicate the records

In this post I am going to show how to generate duplicate recoreds based on the column values in SQL server table
CREATE TABLE Repeat_Table
(
   Item           VARCHAR(20) ,
   Desccription   VARCHAR(20),
   Quantity       INT,  
   Amount         VARCHAR(20)
)
INSERT INTO Repeat_Table
VALUES
('I_001','Description 001', 3,'1000'),
('I_002','Description 002', 4,'5000'),
('I_003','Description 003', 2,'7000')
image
Once you inserted above test data you can genrate reduplicate records by using below qeury.
;WITH CTE (Vals)
AS (
    SELECT 1
     UNION ALL
    SELECT 1 + Vals
    FROM CTE WHERE Vals < 99
   )
SELECT A.[Item]
      , A.[Desccription]
      ,1 AS [Quantity]
      ,A.[Amount]
   
FROM   [Test].[dbo].[Repeat_Table]  A
INNER JOIN CTE C   ON C.Vals <= A.[Quantity]
ORDER BY A.[Item]
1
SQL Server / T-SQL :- Based on the Column value duplicate the records SQL Server / T-SQL :- Based on the Column value duplicate the records Reviewed by Pubudu Dewagama on 10:27:00 PM Rating: 5

No comments: