0

如果以前的条目不为空,我正在尝试遍历表并更新当前为空的信息。下面的代码是我想出的,但这只是更新每个字段,以便每个联系人都有相同的传真号码。比我聪明的人知道该怎么做吗?

谢谢您的帮助。

UPDATE dbo.AAASTA_CONTACT
SET 
CON_CSN = COALESCE(CRNT_RCD.CON_CSN, LST_RCD.CON_CSN),
CON_COMMENTS = COALESCE(CRNT_RCD.CON_COMMENTS, LST_RCD.CON_COMMENTS),
CON_EMAIL_ADDR = COALESCE(CRNT_RCD.CON_EMAIL_ADDR, LST_RCD.CON_EMAIL_ADDR),
CON_FAX_PH_NUM = COALESCE(CRNT_RCD.CON_FAX_PH_NUM, LST_RCD.CON_FAX_PH_NUM),
CON_HOME_PH_NUM = COALESCE(CRNT_RCD.CON_HOME_PH_NUM, LST_RCD.CON_HOME_PH_NUM),
CON_SEX_MF = COALESCE(CRNT_RCD.CON_SEX_MF, LST_RCD.CON_SEX_MF),
CON_PER_TITLE = COALESCE(CRNT_RCD.CON_PER_TITLE, LST_RCD.CON_PER_TITLE),
CON_WORK_PH_NUM = COALESCE(CRNT_RCD.CON_WORK_PH_NUM, LST_RCD.CON_WORK_PH_NUM),
CON_SUPPRESSMAILFL = COALESCE(CRNT_RCD.CON_SUPPRESSMAILFL, LST_RCD.CON_SUPPRESSMAILFL)
FROM
(
SELECT
CON.ALIAS_KEY, 
CON.LAST_UPD, 
CON.CON_CSN, 
CON.CON_COMMENTS, 
CON.CON_EMAIL_ADDR, 
CON.CON_FAX_PH_NUM, 
CON.CON_HOME_PH_NUM, 
CON.CON_SEX_MF, 
CON.CON_PER_TITLE, 
CON.CON_WORK_PH_NUM,
CON.CON_SUPPRESSMAILFL,
RANK() OVER (PARTITION BY CON.ALIAS_KEY ORDER BY CON.LAST_UPD) AS UPD_RNK
FROM dbo.AAASTA_CONTACT CON
) CRNT_RCD
LEFT OUTER JOIN
(
SELECT
CON.ALIAS_KEY, 
CON.LAST_UPD, 
CON.CON_CSN, 
CON.CON_COMMENTS, 
CON.CON_EMAIL_ADDR, 
CON.CON_FAX_PH_NUM, 
CON.CON_HOME_PH_NUM, 
CON.CON_SEX_MF, 
CON.CON_PER_TITLE, 
CON.CON_WORK_PH_NUM,
CON.CON_SUPPRESSMAILFL,
RANK() OVER (PARTITION BY CON.ALIAS_KEY ORDER BY CON.LAST_UPD) AS UPD_RNK
FROM dbo.AAASTA_CONTACT CON
) LST_RCD
ON CRNT_RCD.ALIAS_KEY = LST_RCD.ALIAS_KEY
WHERE CRNT_RCD.UPD_RNK=1
AND LST_RCD.UPD_RNK>1
4

0 回答 0