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 8396503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:33:15+00:00 2026-06-09T20:33:15+00:00

I have been researching dynamic joins for a few hours now and am having

  • 0

I have been researching dynamic joins for a few hours now and am having an issue figuring this out. I am bulk loading XML feed data into a temporary SQL table and then adding/updating the production table from the temp table. However, I am also attempting to use conditional logic in order to preserve data when necessary. The code below is basically a replication of the Merge() method that is only available in SQL Server 2008 and above (the production server is 2005). The conditional logic, specifically, that I need to implement in this query is that if the [eMail] field of the temp table is not blank, then update that field in the production table. Otherwise, do not update the [eMail] field in the production server (in order to preserve data). Here is what I have tried, and does not work due to incorrect syntax:

UPDATE rt
SET rt.[AccountID] = tmp.[AccountID],
  rt.[CustNbr] = tmp.[CustNbr],
  rt.[ClientID] = tmp.[ClientID],
  rt.[ClientX] = tmp.[ClientX],
  rt.[CorpID] = tmp.[CorpID],
  rt.[fName] = tmp.[fName],
  rt.[lName] = tmp.[lName],
  rt.[Position] = tmp.[Position],
  rt.[eMail] = tmp.[eMail],
  rt.[ClientNotes] = tmp.[ClientNotes],
  rt.[DeptID] = tmp.[DeptID]
 FROM AccountClient rt
 IF(SELECT eMail FROM AccountClientTemp) <> ''
     INNER JOIN AccountClientTemp tmp
        ON  tmp.[fName] = rt.[fName]
        and tmp.[lName] = rt.[lName]
        --and tmp.[eMail] = rt.[eMail]
        and tmp.[Position] = rt.[Position]
        and tmp.[DeptID] = rt.[DeptID]
ELSE
    INNER JOIN AccountClientTemp tmp
        ON  tmp.[fName] = rt.[fName]
        and tmp.[lName] = rt.[lName]
        and tmp.[eMail] = rt.[eMail]
        and tmp.[Position] = rt.[Position]
        and tmp.[DeptID] = rt.[DeptID]
INSERT INTO AccountClient
        ([AccountID], [CustNbr], [ClientID], [CorpID], [fName], [lName], [Position],
         [eMail], [ClientNotes], [DeptID])
        SELECT  tmp.[AccountID], tmp.[CustNbr], tmp.[ClientID], tmp.[CorpID], tmp.[fName], tmp.[lName],
        tmp.[Position], tmp.[eMail], tmp.[ClientNotes], tmp.[DeptID]
        FROM AccountClientTemp tmp
        LEFT JOIN AccountClient rt
            ON tmp.[fName] = rt.[fName]
            and tmp.[lName] = rt.[lName]
            and tmp.[eMail] = rt.[eMail]
            and tmp.[Position] = rt.[Position]
        WHERE rt.[fName] IS NULL
         and rt.[lName] IS NULL
         and rt.[eMail] IS NULL
         and rt.[Position] IS NULL
  • 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-06-09T20:33:16+00:00Added an answer on June 9, 2026 at 8:33 pm

    You can’t put an “if” statement in the middle of a query. So, your update should look like:

    UPDATE rt
        SET rt.[AccountID] = tmp.[AccountID],
            rt.[CustNbr] = tmp.[CustNbr],
            rt.[ClientID] = tmp.[ClientID],
            rt.[ClientX] = tmp.[ClientX],
            rt.[CorpID] = tmp.[CorpID],
            rt.[fName] = tmp.[fName],
            rt.[lName] = tmp.[lName],
            rt.[Position] = tmp.[Position],
            rt.[eMail] = (case when tmp.[eMail] = '' then rt.eMail else tmp.eMail end),
            rt.[ClientNotes] = tmp.[ClientNotes],
            rt.[DeptID] = tmp.[DeptID]
        FROM AccountClient rt
             INNER JOIN AccountClientTemp tmp
             ON  tmp.[fName] = rt.[fName] and
                 tmp.[lName] = rt.[lName] and
                 (tmp.[eMail] = rt.[eMail] or tmp.eMail = '') and
                 tmp.[Position] = rt.[Position] and
                 tmp.[DeptID] = rt.[DeptID];
    

    On first glance, the INSERT looks ok.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been researching this topic for a few days now and i'm still
I have been researching this issue for a couple of weeks now. Note in
I have been researching this issue pretty extensively and cannot seem to find an
I have been researching this issue for long. But I haven't come across any
I have been researching this problem for a while now and I just can't
I have been researching all over the internet about this, and unfortunately cannot find
I have been researching this for a while but got no convinced answer. From
I have been researching a few different apps lately for my company which are
I have been researching this for the past hour, and yet to come up
So i have been researching this for quite a while, and found nothing? I

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.