• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Insert Duplicate Record with change in only one field

I need to add records to a table that will be duplicates of other records except that only one value will be different.

So, adding records with FulID that already exists in table but making the FulQId = 229 in the all the new records.

I know I should probably use a CURSOR but I'm not sure how.

Heres what I have so far:



WHILE NOT (SELECT DISTINCT FulID FROM wkbFulResp WHERE FulQTypeId = 1) = Null 
BEGIN

INSERT INTO wkbFulResp 
     SELECT FulRespId, FulRespStatus, FulId, FulQTypeId, '229' AS FulQId,
                 QuestionCatId, FulResp, RespComments, Created, CreatedBy,
                 Modified, ModifiedBy,Reason
                 FROM dbo.wkbFulResp WHERE FulId = FulId

END

Open in new window


In the Where clause  the FulId should be the FulId in the current loop...
So, WHERE FulId = [The FulId in the current loop]


This is the best I can explain...
0
rmosher315
Asked:
rmosher315
  • 3
  • 3
1 Solution
 
Jakub BaumanCommented:
Will not simple query like this, do what you need:

INSERT INTO wkbFulResp 
     SELECT FulRespId, FulRespStatus, FulId, FulQTypeId, '229' AS FulQId,
                 QuestionCatId, FulResp, RespComments, Created, CreatedBy,
                 Modified, ModifiedBy,Reason
                 FROM dbo.wkbFulResp WHERE FulQTypeId = 1

Open in new window

0
 
rmosher315Author Commented:
But will that add 1 record for each distinct FulId in the table?
0
 
Jakub BaumanCommented:
What do you mean distinct FulId? If you have more record with the same FulID which one would you like to duplicate ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rmosher315Author Commented:
It doesn't matter which one.  

If there is more than one record with the same FulId, they will always have the same data excpet a different FulQId.  

So, for each new record all fields except FulQId would be duplicated.

However, if I have 10 records with the same FulId, I don't want to duplicate all 10, just one and populate the FulQId with the number '229'.

This is the reason to Select Distinct FulIds from the table where the TYPE (FulQTypeId) is 1, then loop through, duplicating all fields except FulQId which will be populated with 229.
0
 
Jakub BaumanCommented:
So you can use distinct for all fields except FulQid which will be set as constant value:

INSERT INTO wkbFulResp 
     SELECT DISTINCT FulRespId, FulRespStatus, FulId, FulQTypeId, '229' AS FulQId,
                 QuestionCatId, FulResp, RespComments, Created, CreatedBy,
                 Modified, ModifiedBy,Reason
                 FROM dbo.wkbFulResp WHERE FulQTypeId = 1

Open in new window

0
 
rmosher315Author Commented:
Thank you that worked perfectly.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now