SQL Server - Merging one record to another and changing items referencing the first item to reference the...












0















I have a duplicate record in a SQL Server table with (obviously) different unique IDs.



 CustomerId FirstName LastName| 
1 John Doe |
2 John Doe |


There are child items that are already in the database (let's say an orders table), some relate to one of the items and some relate to the other.



OrderId  CustomerId  DateEntered|
100 1 2018/11/01 |
101 2 2018/11/09 |


They are both referring to the same customer.
I would like to delete the duplicate record. Is there a way to automatically update the CustomerIds in the Orders table that has a value of 2 to have a value of 1?










share|improve this question

























  • Sample data and expected results as formatted text please not images.

    – Sami
    Nov 18 '18 at 20:02






  • 1





    The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.

    – Jason A. Long
    Nov 18 '18 at 21:13


















0















I have a duplicate record in a SQL Server table with (obviously) different unique IDs.



 CustomerId FirstName LastName| 
1 John Doe |
2 John Doe |


There are child items that are already in the database (let's say an orders table), some relate to one of the items and some relate to the other.



OrderId  CustomerId  DateEntered|
100 1 2018/11/01 |
101 2 2018/11/09 |


They are both referring to the same customer.
I would like to delete the duplicate record. Is there a way to automatically update the CustomerIds in the Orders table that has a value of 2 to have a value of 1?










share|improve this question

























  • Sample data and expected results as formatted text please not images.

    – Sami
    Nov 18 '18 at 20:02






  • 1





    The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.

    – Jason A. Long
    Nov 18 '18 at 21:13
















0












0








0








I have a duplicate record in a SQL Server table with (obviously) different unique IDs.



 CustomerId FirstName LastName| 
1 John Doe |
2 John Doe |


There are child items that are already in the database (let's say an orders table), some relate to one of the items and some relate to the other.



OrderId  CustomerId  DateEntered|
100 1 2018/11/01 |
101 2 2018/11/09 |


They are both referring to the same customer.
I would like to delete the duplicate record. Is there a way to automatically update the CustomerIds in the Orders table that has a value of 2 to have a value of 1?










share|improve this question
















I have a duplicate record in a SQL Server table with (obviously) different unique IDs.



 CustomerId FirstName LastName| 
1 John Doe |
2 John Doe |


There are child items that are already in the database (let's say an orders table), some relate to one of the items and some relate to the other.



OrderId  CustomerId  DateEntered|
100 1 2018/11/01 |
101 2 2018/11/09 |


They are both referring to the same customer.
I would like to delete the duplicate record. Is there a way to automatically update the CustomerIds in the Orders table that has a value of 2 to have a value of 1?







sql-server entity-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 20:25







Izzy

















asked Nov 18 '18 at 19:51









IzzyIzzy

464




464













  • Sample data and expected results as formatted text please not images.

    – Sami
    Nov 18 '18 at 20:02






  • 1





    The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.

    – Jason A. Long
    Nov 18 '18 at 21:13





















  • Sample data and expected results as formatted text please not images.

    – Sami
    Nov 18 '18 at 20:02






  • 1





    The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.

    – Jason A. Long
    Nov 18 '18 at 21:13



















Sample data and expected results as formatted text please not images.

– Sami
Nov 18 '18 at 20:02





Sample data and expected results as formatted text please not images.

– Sami
Nov 18 '18 at 20:02




1




1





The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.

– Jason A. Long
Nov 18 '18 at 21:13







The short answer is NO... Nothing that would work automatically. You'd have to find all FK references to "CustomerID" (hopefully the database in question actually has good DRI) and generate commands to update their values.

– Jason A. Long
Nov 18 '18 at 21:13














1 Answer
1






active

oldest

votes


















0














You can use a window function to solve this



WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;


Demo



But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.






share|improve this answer


























  • Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.

    – Izzy
    Nov 19 '18 at 22:07











  • It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.

    – Izzy
    Nov 19 '18 at 22:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364826%2fsql-server-merging-one-record-to-another-and-changing-items-referencing-the-fi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use a window function to solve this



WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;


Demo



But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.






share|improve this answer


























  • Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.

    – Izzy
    Nov 19 '18 at 22:07











  • It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.

    – Izzy
    Nov 19 '18 at 22:09
















0














You can use a window function to solve this



WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;


Demo



But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.






share|improve this answer


























  • Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.

    – Izzy
    Nov 19 '18 at 22:07











  • It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.

    – Izzy
    Nov 19 '18 at 22:09














0












0








0







You can use a window function to solve this



WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;


Demo



But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.






share|improve this answer















You can use a window function to solve this



WITH CTE AS
(
SELECT T1.*,
MIN(T1.CustomerId) OVER(PARTITION BY T1.FirstName, T1.LastName ORDER BY T1.CustomerId) RN
FROM T1
)
UPDATE T2
SET T2.CustomerId = CTE.RN
FROM T2 INNER JOIN CTE ON T2.CustomerId = CTE.CustomerId;


Demo



But in the first place you need to fix this, the customer should have one(1) Unique id, not multiple IDs.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 '18 at 21:17

























answered Nov 18 '18 at 21:12









SamiSami

8,89831241




8,89831241













  • Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.

    – Izzy
    Nov 19 '18 at 22:07











  • It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.

    – Izzy
    Nov 19 '18 at 22:09



















  • Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.

    – Izzy
    Nov 19 '18 at 22:07











  • It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.

    – Izzy
    Nov 19 '18 at 22:09

















Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.

– Izzy
Nov 19 '18 at 22:07





Thank you. What I wanted to know was if SQL Server has built in that the records in the other tables that relate to this record can automatically be updated to the record it was merged into without specifically updating the CustomerId in the record in T2.

– Izzy
Nov 19 '18 at 22:07













It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.

– Izzy
Nov 19 '18 at 22:09





It also does happen as sometimes someone adds a customer because he spelled it differently than the one in the database and thinks that it does not exist yet.

– Izzy
Nov 19 '18 at 22:09




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364826%2fsql-server-merging-one-record-to-another-and-changing-items-referencing-the-fi%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini