Duplicate records except one column

2

Good morning everyone, I'm trying to duplicate these 3 records to another user

This is the code that gets the result of the image.

CREATE TABLE #TEMP (IDSOL INT not null PRIMARY KEY ,USUARIO VARCHAR(50) ,LastName varchar(255) NOT NULL,FirstName varchar(255),Age int, )

INSERT INTO #TEMP 
VALUES(1,'ADMIN','RODRIGUEZ','LOPEZ',20)
INSERT INTO #TEMP 
VALUES(2,'ADMIN','YAÑEZ','CARROL',20)
INSERT INTO #TEMP 
VALUES(3,'ADMIN','MONS','JAFET',20)

SELECT * FROM #TEMP


--DROP TABLE #TEMP

Try to duplicate it as it is with a different user, just that the IDSOL column is PRIMARY KEY, so you can not repeat the same value, could you give me an idea? I was thinking about using a WHILE and inserting registry by registry but I do not have a clear idea.

    
asked by ARR 25.01.2018 в 18:47
source

3 answers

2

Ideally.

The definition of your table must be a IDENTITY for IDSOL which is your primary key, so you only have to make a simple INSERT from a SELECT :

CREATE TABLE #TEMP (IDSOL INT not null IDENTITY(1,1) PRIMARY KEY ,USUARIO VARCHAR(50) ,LastName varchar(255) NOT NULL,FirstName varchar(255),Age int, )

INSERT INTO #TEMP VALUES('ADMIN','RODRIGUEZ','LOPEZ',20) 
INSERT INTO #TEMP VALUES('ADMIN','YAÑEZ','CARROL',20)
INSERT INTO #TEMP VALUES('ADMIN','MONS','JAFET',20)

INSERT INTO #TEMP
SELECT 'USUARIO', LastName, FirstName, Age FROM #TEMP GROUP BY IDSOL, LastName, FirstName, Age

SELECT * FROM #TEMP

The resutlado will be the following:

+---+-------+---------+-----------+-----------+-----+
|   | IDSOL | USUARIO | LastName  | FirstName | Age |
+---+-------+---------+-----------+-----------+-----+
| 1 | 1     | ADMIN   | RODRIGUEZ | LOPEZ     | 20  |
+---+-------+---------+-----------+-----------+-----+
| 2 | 2     | ADMIN   | YAÑEZ     | CARROL    | 20  |
+---+-------+---------+-----------+-----------+-----+
| 3 | 3     | ADMIN   | MONS      | JAFET     | 20  |
+---+-------+---------+-----------+-----------+-----+
| 4 | 4     | USUARIO | RODRIGUEZ | LOPEZ     | 20  |
+---+-------+---------+-----------+-----------+-----+
| 5 | 5     | USUARIO | YAÑEZ     | CARROL    | 20  |
+---+-------+---------+-----------+-----------+-----+
| 6 | 6     | USUARIO | MONS      | JAFET     | 20  |
+---+-------+---------+-----------+-----------+-----+

Here you can see the demo and its results .

The inevitable.

If for some reason it is impossible to modify the structure of your temporary table, one option is to calculate the last IDSOL inserted by adding the registration number with the function ROW_NUMBER , where you simply use the same code that you have and the INSERT is through a% % co:

INSERT INTO #TEMP
SELECT (SELECT MAX(IDSOL) FROM #TEMP) + ROW_NUMBER() OVER (Order by IDSOL), 'USUARIO', LastName, FirstName, Age
FROM #TEMP
GROUP BY IDSOL, LastName, FirstName, Age

The result would be the following:

+---+-------+---------+-----------+-----------+-----+
|   | IDSOL | USUARIO | LastName  | FirstName | Age |
+---+-------+---------+-----------+-----------+-----+
| 1 | 1     | ADMIN   | RODRIGUEZ | LOPEZ     | 20  |
+---+-------+---------+-----------+-----------+-----+
| 2 | 2     | ADMIN   | YAÑEZ     | CARROL    | 20  |
+---+-------+---------+-----------+-----------+-----+
| 3 | 3     | ADMIN   | MONS      | JAFET     | 20  |
+---+-------+---------+-----------+-----------+-----+
| 4 | 4     | USUARIO | RODRIGUEZ | LOPEZ     | 20  |
+---+-------+---------+-----------+-----------+-----+
| 5 | 5     | USUARIO | YAÑEZ     | CARROL    | 20  |
+---+-------+---------+-----------+-----------+-----+
| 6 | 6     | USUARIO | MONS      | JAFET     | 20  |
+---+-------+---------+-----------+-----------+-----+

Here you can see the demo and its results .

    
answered by 25.01.2018 / 20:46
source
2

You could always get the max idsol + 1 each time you want to insert the following:

DECLARE @id INT 


SELECT @id = MAX(idsol) + 1 FROM #TEMP

 INSERT INTO #TEMP 
VALUES(@id,'ADMIN','RODRIGUEZ','LOPEZ',20)

 SELECT @id = MAX(idsol) + 1 FROM #TEMP

INSERT INTO #TEMP 
VALUES(@id,'ADMIN','YAÑEZ','CARROL',20)

 SELECT @id = MAX(idsol) + 1 FROM #TEMP

INSERT INTO #TEMP 
VALUES(@id,'ADMIN','MONS','JAFET',20) 

This way you make sure to always insert the next id to the last generated.

    
answered by 25.01.2018 в 19:17
0

Regarding what you mention, that you do not have the possibility of modifying the table, what occurs to me is that you can get the last ID entered and increase it. So you avoid duplicity. I imagine you can use MAX .

SELECT MAX(IDSOL) FROM tu_tabla;
    
answered by 25.01.2018 в 19:15