Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 727661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:35:35+00:00 2026-05-14T06:35:35+00:00

I am trying work out with MERGE statment to Insert / Update Dimension Table

  • 0

I am trying work out with MERGE statment to Insert / Update Dimension Table of Type SCD2

My source is a Table var to Merge with Dimension table.

My MERGE statement is throwing an error as:

The target table
‘DM.DATA_ERROR.ERROR_DIMENSION’ of the
INSERT statement cannot be on either
side of a (primary key, foreign key)
relationship when the FROM clause
contains a nested INSERT, UPDATE,
DELETE, or MERGE statement. Found
reference constraint
‘FK_ERROR_DIMENSION_to_AUDIT_CreatedBy’.

My MERGE Statement:

DECLARE @DATAERROROBJECT AS [ERROR_DIMENSION]

INSERT INTO DM.DATA_ERROR.ERROR_DIMENSION
SELECT  ERROR_CODE,
    DATA_STREAM_ID,
    [ERROR_SEVERITY],
    DATA_QUALITY_RATING,
    ERROR_LONG_DESCRIPTION,
    ERROR_DESCRIPTION,
    VALIDATION_RULE,
    ERROR_TYPE,
    ERROR_CLASS,
    VALID_FROM,
    VALID_TO,
    CURR_FLAG,
    CREATED_BY_AUDIT_SK,
    UPDATED_BY_AUDIT_SK

FROM (MERGE DM.DATA_ERROR.ERROR_DIMENSION ED USING @DATAERROROBJECT OBJ
     ON(ED.ERROR_CODE = OBJ.ERROR_CODE AND ED.DATA_STREAM_ID = OBJ.DATA_STREAM_ID)     WHEN NOT MATCHED THEN    INSERT VALUES(
         OBJ.ERROR_CODE
        ,OBJ.DATA_STREAM_ID
        ,OBJ.[ERROR_SEVERITY]
        ,OBJ.DATA_QUALITY_RATING
        ,OBJ.ERROR_LONG_DESCRIPTION
        ,OBJ.ERROR_DESCRIPTION
        ,OBJ.VALIDATION_RULE
        ,OBJ.ERROR_TYPE
        ,OBJ.ERROR_CLASS
        ,GETDATE()
        ,'9999-12-13'
        ,'Y'
        ,1
        ,1
        ) WHEN MATCHED AND ED.CURR_FLAG = 'Y'
             AND (      ED.[ERROR_SEVERITY] <> OBJ.[ERROR_SEVERITY]
                   OR   ED.[DATA_QUALITY_RATING] <> OBJ.[DATA_QUALITY_RATING]
                   OR   ED.[ERROR_LONG_DESCRIPTION] <> OBJ.[ERROR_LONG_DESCRIPTION]
                   OR   ED.[ERROR_DESCRIPTION] <> OBJ.[ERROR_DESCRIPTION]
                   OR   ED.[VALIDATION_RULE] <> OBJ.[VALIDATION_RULE]
                   OR   ED.[ERROR_TYPE] <> OBJ.[ERROR_TYPE]
                   OR   ED.[ERROR_CLASS] <> OBJ.[ERROR_CLASS] ) THEN UPDATE SET ED.CURR_FLAG = 'N', ED.VALID_TO = GETDATE()
        OUTPUT $ACTION ACTION_OUT, 
                           OBJ.ERROR_CODE ERROR_CODE,
                           OBJ.DATA_STREAM_ID DATA_STREAM_ID,
                           OBJ.[ERROR_SEVERITY] [ERROR_SEVERITY],
                           OBJ.DATA_QUALITY_RATING DATA_QUALITY_RATING,
                           OBJ.ERROR_LONG_DESCRIPTION ERROR_LONG_DESCRIPTION,
                           OBJ.ERROR_DESCRIPTION ERROR_DESCRIPTION,
                           OBJ.VALIDATION_RULE VALIDATION_RULE,
                           OBJ.ERROR_TYPE ERROR_TYPE,
                           OBJ.ERROR_CLASS ERROR_CLASS,
                           GETDATE() VALID_FROM,
                           '9999-12-31' VALID_TO,
                           'Y' CURR_FLAG,
                           555 CREATED_BY_AUDIT_SK,
                           555 UPDATED_BY_AUDIT_SK
                           ) AS MERGE_OUT WHERE MERGE_OUT.ACTION_OUT = 'UPDATE';

What am I doing wrong ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:35:35+00:00Added an answer on May 14, 2026 at 6:35 am

    I think your fundamental error is the fact that you seem to expect that MERGE is returning a data set – like SELECT …. does – it does not.

    I don’t believe you can use

    SELECT (list of fields)
    FROM (MERGE DM.DATA_ERROR.ERROR_DIMENSION ED 
          USING @DATAERROROBJECT OBJ
          ON(ED.ERROR_CODE = OBJ.ERROR_CODE AND ED.DATA_STREAM_ID = OBJ.DATA_STREAM_ID)
       .......
    

    What you need to do is:

    • load your data to be merged into a separate staging table
    • then issue a MERGE statement as a separate statement against the target table, using that staging table as your source

    Check out these resources:

    • Using SQL Server 2008 MERGE statement
    • SQL Server 2008: Introduction to MERGE
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to work out how I can insert the string End into
I am trying to work out an IF statement and getting lost. I am
Im trying to work out how to get the value from one setting in
I trying to work out what regex will match filenames in quotes. Eg. blah
i am trying to work out the following problem in Java (although it could
I am trying to work out why I can not access the nested function
I am trying to work out the best practice for my problem. I have
I'm trying to work out the correct pattern to use for a Singleton that
I'm trying to work out the ControlTemplate.Triggers in the Style below, and I haven't
This array_merge doesn't seem to work for me. I am trying merge all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.