Solved

Insert Duplicate Record with change in only one field

Posted on 2012-04-11
6
360 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
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.

 

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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

770 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