[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Insert Duplicate Record with change in only one field

Posted on 2012-04-11
6
Medium Priority
?
371 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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