Solved

Insert Duplicate Record with change in only one field

Posted on 2012-04-11
6
356 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
  • 3
  • 3
6 Comments
 
LVL 1

Expert Comment

by:jakubbauman
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:jakubbauman
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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:
jakubbauman 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now