SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for ASP.Net, SQL Server, Reporting Services, T-SQL, Windows, AWS, SAP HANA and ABAP

SQL Repeat Rows N Times According to Column Value

To repeat table rows in different number of times SQL developers can use given T-SQL codes in this SQL tutorial. Sometimes developers are required to repeat records according to a number column in that table rows.
For example, you have a master table and sql developers should create new repeating rows in an other database table for that source row. Assume that the number of rows to insert are varying for each source row according to a numeric table column in the source table. In this case given sql codes in this tutorial will help SQL Server developers coding in SQL to repeat rows.

RepeatRows is a sample database table where the source rows are stored. The requested repeating number is stored in the column named RepeatColumn in the database table RepeatRows.

 Id int identity(1,1),
 RepeatText varchar(100),
 RepeatCount int
INSERT INTO RepeatRows SELECT 'Repeat 3 Times', 3
INSERT INTO RepeatRows SELECT 'Repeat 2 Times', 2
INSERT INTO RepeatRows SELECT 'Repeat 4 Times', 4
INSERT INTO RepeatRows SELECT 'Only once', 1

SELECT * FROM RepeatRows

You can see the database table populated with sample data in below screenshot.

sql rows to repeat

Now with the help of a SQL numbers table, or a SQL user function which returns a temporary numbers table I created the following SQL SELECT statement. Please note the use of CROSS APPLY operator which joins the RepeatRows table and data returning from SQL numbers table function NumbersTable. The trick in this SQL code is passing the RepeatCount column value of the source table to the SQL function NumbersTable. And this SQL Select will repeat rows according to the count column in the same table record.

Please refer to Create SQL numbers table in SQL Server for the source codes of the user function dbo.NumbersTable used in this Transact-SQL tutorial.

SELECT * -- Id, RepeatText, RepeatCount
FROM RepeatRows r
CROSS APPLY dbo.NumbersTable(1,r.RepeatCount,1) --n

The output of the above SQL statement is as follows. As you see each source row is repeated n times (according to the RepeatCount column value of the source row) in the return set of the SELECT statement.

repeat rows n times using numbers table and Cross Apply

An other repeating records sample case can be like this. Assume that we have a computer cource class where students are taking exams. The number of the students taking course exams are stored in database table named Exams. And we will store exam scores in target database table ExamScores.

Here is the DDL codes for the sql tables and script for data generation. We want to repeat rows stored in this table n times defined in the NumberOfStudent numeric column value.

 Id int identity(1,1),
 Title nvarchar(100),
 ExamDate datetime,
 NumberOfStudents smallint

INSERT INTO Exams SELECT '70-432 SQL Server 2008, Implementation and Maintenance', '20110301', 10
INSERT INTO Exams SELECT '70-433 SQL Server 2008, Database Development', '20110302', 5

Now we have two exam records in our source table. Let's insert new rows for the exam scores for the exam candidates in ExamScores table by repeating the rows as the number of exam takers in source table.

Again for multiplying records and repeating them for a specific number of times, I'll use the SQL SELECT statement in combination with NumbersTable sql function and CROSS APPLY operator. Here is the DDL source code for ExamScores table.

 Id int identity(1,1),
 ExamId int not null,
 ExamDate datetime not null,
 StudentName nvarchar(100),
 Score smallint

And the below SQL codes repeat source row with the number of times defined in a column of that table row.

INSERT INTO ExamScores (ExamId, ExamDate)
SELECT Id, ExamDate
FROM Exams
CROSS APPLY dbo.NumbersTable(1,NumberOfStudents,1)

I hope this SQL tutorial helps SQL programmers repeat a table row n times using SQL Server Numbers table and Cross Apply join. I believe this tutorial will give an idea how to use Cross Apply in their SQL development for data professionals

SQL Server

SQL Server 2019 Installation
download SQL Server 2019
download SQL Server 2017
download SQL Server 2016
download SQL Server 2014
download SQL Server 2012
MacOS ve SQL Server 2019

Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.