1

我正在寻找一种在 SQL Server 中将这两个语句组合成一个的方法。我尝试搜索“条件连接”,但没有看到这个确切的例子感谢您的帮助。

update
    employees
set
    employees.RefID = ed.RefID
from
    #tmpEmployees employees 
    inner join EmployeeDirectory ed on ed.ABC = employees.ABC
where
    ( nullif(ed.ABC, '') is not null and  nullif(employees.ABC, '') is not null)



update
    employees
set
    employees.RefID = ed.RefID
from
    #tmpEmployees employees 
    inner join EmployeeDirectory ed on ed.DEF = employees.DEF
where
    ( nullif(ed.DEF, '') is not null and  nullif(employees.DEF, '') is not null)
4

2 回答 2

0

我不确定我是否完全理解您要完成的工作,但也许以下内容会有所帮助:

UPDATE
    employees
SET
    employees.RefID = ed.RefID
FROM
    #tmpEmployees employees 
JOIN EmployeeDirectory ed 
  ON ed.ABC = employees.ABC OR
     ed.ABC IS NULL AND employees.ABC IS NULL OR
     ed.DEF = employees.DEF OR
     ed.DEF IS NULL AND employees.DEF IS NULL

如果NULL值处理不是您想要的(或与您想要的相反),您可以修改/反转它。

于 2013-08-14T14:53:13.670 回答
0

尝试

UPDATE e
   SET e.RefID = d.RefID
  FROM #tmpEmployees e JOIN EmployeeDirectory d 
    ON e.ABC = d.ABC OR e.DEF = d.DEF
 WHERE NULLIF(e.ABC, '') IS NOT NULL 
   AND NULLIF(d.ABC, '') IS NOT NULL
   AND NULLIF(d.DEF, '') IS NOT NULL
   AND NULLIF(e.DEF, '') IS NOT NULL

或者

UPDATE e
   SET e.RefID = COALEASCE(d1.RefID, d2.RefID)
  FROM #tmpEmployees e LEFT JOIN EmployeeDirectory d1 
    ON e.ABC = d1.ABC LEFT JOIN EmployeeDirectory d2 
    ON e.DEF = d2.DEF
于 2013-08-14T14:56:51.887 回答