Merge join not able to join properly on varchar column
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
add a comment |
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
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 theOUTPUT
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 seperateINSERT
andUPDATE
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 duplicatecsname
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
add a comment |
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
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
tsql
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 theOUTPUT
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 seperateINSERT
andUPDATE
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 duplicatecsname
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
add a comment |
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 theOUTPUT
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 seperateINSERT
andUPDATE
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 duplicatecsname
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
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 seperateINSERT
andUPDATE
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