Question

I wish to replicate a set of rows in a table but with handling unique column in SQL Server 17. The table is:

CREATE TABLE test1 (
    id int CONSTRAINT PK_test1 PRIMARY KEY  CLUSTERED IDENTITY,
    name nvarchar(10) CONSTRAINT UK_test1 UNIQUE NONCLUSTERED,
    value int,
    type varchar(20)
);

The table contents are:

INSERT INTO test1 VALUES ('e_1',1,'A');
INSERT INTO test1 VALUES ('342A',34,'ABC');
INSERT INTO test1 VALUES ('Tame_33',277,'Test10');

Now I wish to replicate those rows multiple times but the problem is with NAME column which is UNIQUE. The solution I did so far is just use of a cursor, but this is inefficient and I'm curious if this can be done in more efficient way?

DECLARE @id int;
DECLARE @name nvarchar(20);
DECLARE @value int;
DECLARE @type varchar(10);
DECLARE @cnt int = 0;stackoverflow
DECLARE @intcnt int = 0;

DECLARE temp_cursor CURSOR FOR
SELECT * FROM test1
OPEN temp_cursor;
FETCH NEXT FROM temp_cursor INTO @id,@name,@value,@type;

WHILE @@FETCH_STATUS = 0
BEGIN
    WHILE @intcnt < 5
    BEGIN
        INSERT INTO test1 VALUES
        ('ins_' + CONVERT(varchar,@intcnt) + '_'  + CONVERT(varchar,@cnt), @value, @type);
        SET @intcnt = @intcnt + 1;
    END 

    SET @intcnt = 0;
    
    FETCH NEXT FROM temp_cursor INTO @id,@name,@value,@type;
    SET @cnt = @cnt + 1;
END

CLOSE temp_cursor;
DEALLOCATE temp_cursor;
Was it helpful?

Solution

WITH gen AS (
    SELECT 1 AS num
    UNION ALL
    SELECT num+1 FROM gen WHERE num+1<=5
)
INSERT INTO test1 (name,value,type)
SELECT 'ins_' + CONVERT(varchar,num) + '_'  + CONVERT(varchar,id), value, type 
FROM gen
CROSS JOIN test1

Some explanation provided by the OP, added so others can adapt the solution:

Actually used CTE as a generator of integers (called 'fnTally') which he later used in CROSS JOIN to produce cartesian product of those two tables - the source one which is 'test1' and generated by CTE called 'gen' (a combination of every row from two tables). The only further optimization here would be to use cCTE (cascade CTE introduced by Itzik Ben-Gan).

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top