简体   繁体   中英

SQL - Possible Dynamic SQL Join Issue?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM