Merge join not able to join properly on varchar column









up vote
0
down vote

favorite












I've created below code to implement SCD type 2 using merge, when i run the code i'm getting primary key violations on csname field. I have the below values as part of primary key, not sure whether merge SQL does support for varchar or not.
if I run the normal inner join SQL on the same key then i'm getting the matching records as well.



Any help much appreciated



csname
ER - Building Complaints
TR - Building Applications



CREATE PROCEDURE dbo.load_target
AS

BEGIN
INSERT INTO [TR_DW].[enum].[Rt]([csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord])
SELECT[csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord]
FROM
(

MERGE [TR_DW].[enum].[Rt] RtCSQSuTT
USING [TR].[enum].[Rt] RtCSQSuST
ON (RtCSQSuTT.csname = RtCSQSuST.csname)

WHEN NOT MATCHED THEN
INSERT ([csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord])
VALUES ([csname],[enddatetime],'12/31/9999', getdate(), 'Y')
WHEN MATCHED AND RtCSQSuTT.[CurrentRecord] = 'Y' AND
(ISNULL(RtCSQSuTT.[enddatetime], '') != ISNULL(RtCSQSuST.[enddatetime], ''))THEN
UPDATE SET
RtCSQSuTT.[CurrentRecord] = 'N',
RtCSQSuTT.[EffectiveFromDate] = GETDATE() - 1,
RtCSQSuTT.[EffectiveToDate] = GETDATE()
OUTPUT $Action Action_Taken,RtCSQSuST.[csqname],RtCSQSuST.[enddatetime],'12/31/9999' AS[EffectiveToDate],GETDATE() AS[EffectiveFromDate],'Y' AS[CurrentRecord]
)AS MERGE_OUT21
WHERE MERGE_OUT21.Action_Taken = 'UPDATE';

END
GO









share|improve this question























  • It looks to my like you're inserting twice. First within the merge you insert when not matching. Then any result (inserted or updated) is pushed through from the OUTPUT clause and is inserted again for the outside insert. Can you clarify - did you write this code? If so what was the purpose of this? Personally I usually prefer to use seperate INSERT and UPDATE statements as it is easier to performance tune and debug
    – Nick.McDermaid
    Nov 11 at 23:45











  • basically i need to maintain the history (SCD type 2), so first inset for if the record is not exists and if exits expire the existing record and insert the new record from the source
    – NPK
    Nov 12 at 0:14










  • First make sure there are no duplicate csname in [tr].[enum].[rt]. Also I would be wary of this: ISNULL(RtCSQSuTT.[enddatetime], ''). You're casting dates to strings there - bad idea.
    – Nick.McDermaid
    Nov 12 at 0:28











  • Thank you Nick, what's the best way to compare the dates ?
    – NPK
    Nov 12 at 12:36











  • Convert it to a legitimate date like 1900-01-01, not a string.
    – Nick.McDermaid
    Nov 12 at 13:05














up vote
0
down vote

favorite












I've created below code to implement SCD type 2 using merge, when i run the code i'm getting primary key violations on csname field. I have the below values as part of primary key, not sure whether merge SQL does support for varchar or not.
if I run the normal inner join SQL on the same key then i'm getting the matching records as well.



Any help much appreciated



csname
ER - Building Complaints
TR - Building Applications



CREATE PROCEDURE dbo.load_target
AS

BEGIN
INSERT INTO [TR_DW].[enum].[Rt]([csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord])
SELECT[csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord]
FROM
(

MERGE [TR_DW].[enum].[Rt] RtCSQSuTT
USING [TR].[enum].[Rt] RtCSQSuST
ON (RtCSQSuTT.csname = RtCSQSuST.csname)

WHEN NOT MATCHED THEN
INSERT ([csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord])
VALUES ([csname],[enddatetime],'12/31/9999', getdate(), 'Y')
WHEN MATCHED AND RtCSQSuTT.[CurrentRecord] = 'Y' AND
(ISNULL(RtCSQSuTT.[enddatetime], '') != ISNULL(RtCSQSuST.[enddatetime], ''))THEN
UPDATE SET
RtCSQSuTT.[CurrentRecord] = 'N',
RtCSQSuTT.[EffectiveFromDate] = GETDATE() - 1,
RtCSQSuTT.[EffectiveToDate] = GETDATE()
OUTPUT $Action Action_Taken,RtCSQSuST.[csqname],RtCSQSuST.[enddatetime],'12/31/9999' AS[EffectiveToDate],GETDATE() AS[EffectiveFromDate],'Y' AS[CurrentRecord]
)AS MERGE_OUT21
WHERE MERGE_OUT21.Action_Taken = 'UPDATE';

END
GO









share|improve this question























  • It looks to my like you're inserting twice. First within the merge you insert when not matching. Then any result (inserted or updated) is pushed through from the OUTPUT clause and is inserted again for the outside insert. Can you clarify - did you write this code? If so what was the purpose of this? Personally I usually prefer to use seperate INSERT and UPDATE statements as it is easier to performance tune and debug
    – Nick.McDermaid
    Nov 11 at 23:45











  • basically i need to maintain the history (SCD type 2), so first inset for if the record is not exists and if exits expire the existing record and insert the new record from the source
    – NPK
    Nov 12 at 0:14










  • First make sure there are no duplicate csname in [tr].[enum].[rt]. Also I would be wary of this: ISNULL(RtCSQSuTT.[enddatetime], ''). You're casting dates to strings there - bad idea.
    – Nick.McDermaid
    Nov 12 at 0:28











  • Thank you Nick, what's the best way to compare the dates ?
    – NPK
    Nov 12 at 12:36











  • Convert it to a legitimate date like 1900-01-01, not a string.
    – Nick.McDermaid
    Nov 12 at 13:05












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've created below code to implement SCD type 2 using merge, when i run the code i'm getting primary key violations on csname field. I have the below values as part of primary key, not sure whether merge SQL does support for varchar or not.
if I run the normal inner join SQL on the same key then i'm getting the matching records as well.



Any help much appreciated



csname
ER - Building Complaints
TR - Building Applications



CREATE PROCEDURE dbo.load_target
AS

BEGIN
INSERT INTO [TR_DW].[enum].[Rt]([csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord])
SELECT[csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord]
FROM
(

MERGE [TR_DW].[enum].[Rt] RtCSQSuTT
USING [TR].[enum].[Rt] RtCSQSuST
ON (RtCSQSuTT.csname = RtCSQSuST.csname)

WHEN NOT MATCHED THEN
INSERT ([csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord])
VALUES ([csname],[enddatetime],'12/31/9999', getdate(), 'Y')
WHEN MATCHED AND RtCSQSuTT.[CurrentRecord] = 'Y' AND
(ISNULL(RtCSQSuTT.[enddatetime], '') != ISNULL(RtCSQSuST.[enddatetime], ''))THEN
UPDATE SET
RtCSQSuTT.[CurrentRecord] = 'N',
RtCSQSuTT.[EffectiveFromDate] = GETDATE() - 1,
RtCSQSuTT.[EffectiveToDate] = GETDATE()
OUTPUT $Action Action_Taken,RtCSQSuST.[csqname],RtCSQSuST.[enddatetime],'12/31/9999' AS[EffectiveToDate],GETDATE() AS[EffectiveFromDate],'Y' AS[CurrentRecord]
)AS MERGE_OUT21
WHERE MERGE_OUT21.Action_Taken = 'UPDATE';

END
GO









share|improve this question















I've created below code to implement SCD type 2 using merge, when i run the code i'm getting primary key violations on csname field. I have the below values as part of primary key, not sure whether merge SQL does support for varchar or not.
if I run the normal inner join SQL on the same key then i'm getting the matching records as well.



Any help much appreciated



csname
ER - Building Complaints
TR - Building Applications



CREATE PROCEDURE dbo.load_target
AS

BEGIN
INSERT INTO [TR_DW].[enum].[Rt]([csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord])
SELECT[csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord]
FROM
(

MERGE [TR_DW].[enum].[Rt] RtCSQSuTT
USING [TR].[enum].[Rt] RtCSQSuST
ON (RtCSQSuTT.csname = RtCSQSuST.csname)

WHEN NOT MATCHED THEN
INSERT ([csname],[enddatetime],[EffectiveToDate],[EffectiveFromDate],[CurrentRecord])
VALUES ([csname],[enddatetime],'12/31/9999', getdate(), 'Y')
WHEN MATCHED AND RtCSQSuTT.[CurrentRecord] = 'Y' AND
(ISNULL(RtCSQSuTT.[enddatetime], '') != ISNULL(RtCSQSuST.[enddatetime], ''))THEN
UPDATE SET
RtCSQSuTT.[CurrentRecord] = 'N',
RtCSQSuTT.[EffectiveFromDate] = GETDATE() - 1,
RtCSQSuTT.[EffectiveToDate] = GETDATE()
OUTPUT $Action Action_Taken,RtCSQSuST.[csqname],RtCSQSuST.[enddatetime],'12/31/9999' AS[EffectiveToDate],GETDATE() AS[EffectiveFromDate],'Y' AS[CurrentRecord]
)AS MERGE_OUT21
WHERE MERGE_OUT21.Action_Taken = 'UPDATE';

END
GO






tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 0:10

























asked Nov 11 at 23:26









NPK

155




155











  • It looks to my like you're inserting twice. First within the merge you insert when not matching. Then any result (inserted or updated) is pushed through from the OUTPUT clause and is inserted again for the outside insert. Can you clarify - did you write this code? If so what was the purpose of this? Personally I usually prefer to use seperate INSERT and UPDATE statements as it is easier to performance tune and debug
    – Nick.McDermaid
    Nov 11 at 23:45











  • basically i need to maintain the history (SCD type 2), so first inset for if the record is not exists and if exits expire the existing record and insert the new record from the source
    – NPK
    Nov 12 at 0:14










  • First make sure there are no duplicate csname in [tr].[enum].[rt]. Also I would be wary of this: ISNULL(RtCSQSuTT.[enddatetime], ''). You're casting dates to strings there - bad idea.
    – Nick.McDermaid
    Nov 12 at 0:28











  • Thank you Nick, what's the best way to compare the dates ?
    – NPK
    Nov 12 at 12:36











  • Convert it to a legitimate date like 1900-01-01, not a string.
    – Nick.McDermaid
    Nov 12 at 13:05
















  • It looks to my like you're inserting twice. First within the merge you insert when not matching. Then any result (inserted or updated) is pushed through from the OUTPUT clause and is inserted again for the outside insert. Can you clarify - did you write this code? If so what was the purpose of this? Personally I usually prefer to use seperate INSERT and UPDATE statements as it is easier to performance tune and debug
    – Nick.McDermaid
    Nov 11 at 23:45











  • basically i need to maintain the history (SCD type 2), so first inset for if the record is not exists and if exits expire the existing record and insert the new record from the source
    – NPK
    Nov 12 at 0:14










  • First make sure there are no duplicate csname in [tr].[enum].[rt]. Also I would be wary of this: ISNULL(RtCSQSuTT.[enddatetime], ''). You're casting dates to strings there - bad idea.
    – Nick.McDermaid
    Nov 12 at 0:28











  • Thank you Nick, what's the best way to compare the dates ?
    – NPK
    Nov 12 at 12:36











  • Convert it to a legitimate date like 1900-01-01, not a string.
    – Nick.McDermaid
    Nov 12 at 13:05















It looks to my like you're inserting twice. First within the merge you insert when not matching. Then any result (inserted or updated) is pushed through from the OUTPUT clause and is inserted again for the outside insert. Can you clarify - did you write this code? If so what was the purpose of this? Personally I usually prefer to use seperate INSERT and UPDATE statements as it is easier to performance tune and debug
– Nick.McDermaid
Nov 11 at 23:45





It looks to my like you're inserting twice. First within the merge you insert when not matching. Then any result (inserted or updated) is pushed through from the OUTPUT clause and is inserted again for the outside insert. Can you clarify - did you write this code? If so what was the purpose of this? Personally I usually prefer to use seperate INSERT and UPDATE statements as it is easier to performance tune and debug
– Nick.McDermaid
Nov 11 at 23:45













basically i need to maintain the history (SCD type 2), so first inset for if the record is not exists and if exits expire the existing record and insert the new record from the source
– NPK
Nov 12 at 0:14




basically i need to maintain the history (SCD type 2), so first inset for if the record is not exists and if exits expire the existing record and insert the new record from the source
– NPK
Nov 12 at 0:14












First make sure there are no duplicate csname in [tr].[enum].[rt]. Also I would be wary of this: ISNULL(RtCSQSuTT.[enddatetime], ''). You're casting dates to strings there - bad idea.
– Nick.McDermaid
Nov 12 at 0:28





First make sure there are no duplicate csname in [tr].[enum].[rt]. Also I would be wary of this: ISNULL(RtCSQSuTT.[enddatetime], ''). You're casting dates to strings there - bad idea.
– Nick.McDermaid
Nov 12 at 0:28













Thank you Nick, what's the best way to compare the dates ?
– NPK
Nov 12 at 12:36





Thank you Nick, what's the best way to compare the dates ?
– NPK
Nov 12 at 12:36













Convert it to a legitimate date like 1900-01-01, not a string.
– Nick.McDermaid
Nov 12 at 13:05




Convert it to a legitimate date like 1900-01-01, not a string.
– Nick.McDermaid
Nov 12 at 13:05

















active

oldest

votes











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',
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%2f53254280%2fmerge-join-not-able-to-join-properly-on-varchar-column%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53254280%2fmerge-join-not-able-to-join-properly-on-varchar-column%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto