Solved

Insert Duplicate Record with change in only one field

Posted on 2012-04-11
6
366 Views
Last Modified: 2012-06-27
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
Comment
Question by:rmosher315
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:Jakub Bauman
ID: 37834534
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
 

Author Comment

by:rmosher315
ID: 37834608
But will that add 1 record for each distinct FulId in the table?
0
 
LVL 1

Expert Comment

by:Jakub Bauman
ID: 37834808
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!

 

Author Comment

by:rmosher315
ID: 37834856
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
 
LVL 1

Accepted Solution

by:
Jakub Bauman earned 500 total points
ID: 37835047
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
 

Author Closing Comment

by:rmosher315
ID: 37835194
Thank you that worked perfectly.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question