Hello everyone, I am 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
I tried to duplicate it as it is with a different user only that the IDSOL column is PRIMARY KEY so the same value cannot be repeated, could someone give me an idea? I was thinking of using a WHILE and insert record by record but not I have a clear idea.
You could always get the max idsol + 1 every time you want to insert like this:
This way you make sure to always insert the next id to the last one generated.
The ideal.
The definition of your table must be a
IDENTITY
soIDSOL
that it is your primary key, this way you will only have to make a simpleINSERT
from aSELECT
:The result will be the following:
Here you can see the demo and its results .
Sucker Punch.
If for some reason it is impossible for you to modify the structure of your temporary table, one option is to calculate the last
IDSOL
inserted by adding the record number with the functionROW_NUMBER
, where you simply use the same code that you have and itINSERT
is through aSELECT
:The result would be the following:
Here you can see the demo and its results .
Regarding what you say, that you don't have the possibility to modify the table, what occurs to me is that you can obtain the last entered ID and increase it. This way you avoid duplication. I imagine you can use
MAX
.