I want to copy or duplicate a row in a table (tblSpills) while defining the the PK column values (2) (tblSpills.Year, tblSpills.Complaint).
I already have a stored procedure that inserts a new row with the custom PK values and every other columns (about 30) is empty. So far I have gotten as far as executing the existing stored procedure to create the new row and grabbed the new PK values as variables for the new Duplicate stored procedure, but I cannot figure out how to copy the remaining columns from one row to the newly created row in the same table.
Thank you for any help you can provide.
You’re not telling us anything about what system / database you’re using – but just guessing if it should be SQL Server, you could do something like this:
Basically, you insert a list of columns into the
tblSpillstable, based on your new values forYearandComplaint, and you take all other column values from that same table from an existing row (based on that WHERE condition you have to define)For the “other columns”: you only need to list those columns that require a value or for which you want to set a specific value. Those columns that already have a sensible DEFAULT constraint defined don’t need to be listed – they’ll automatically get the defined default value when a new row is inserted.