我將如何更改此更新語句,以便如果在GENDERSPELLING
表中找不到該值,那麼GENDER
將設置爲'U'?更改更新語句SQL
UPDATE DWCUST DW
SET GENDER = (SELECT NEW_VALUE FROM GENDERSPELLING GS
WHERE DW.GENDER = GS.INVALID_VALUE)
WHERE GENDER NOT IN ('M', 'F');
我將如何更改此更新語句,以便如果在GENDERSPELLING
表中找不到該值,那麼GENDER
將設置爲'U'?更改更新語句SQL
UPDATE DWCUST DW
SET GENDER = (SELECT NEW_VALUE FROM GENDERSPELLING GS
WHERE DW.GENDER = GS.INVALID_VALUE)
WHERE GENDER NOT IN ('M', 'F');
我認爲這應該工作。
UPDATE DWCUST DW
SET GENDER = (
SELECT CASE WHEN MAX(NEW_VALUE) IS NULL
THEN 'U'
ELSE MAX(NEW_VALUE)
END
FROM GENDERSPELLING GS
WHERE DW.GENDER = GS.INVALID_VALUE
)
WHERE GENDER NOT IN ('M', 'F');
即時獲取第2行錯誤:缺少SET關鍵字 –
@NedKhivju我做了一個改變,你可以檢查它嗎? – RRK
可以使用COALESCE
(或Oracle的NVL
)打開NULL
成'U'
:
update dwcust dw
set gender =
nvl((select new_value from genderspelling gs where dw.gender = gs.invalid_value), 'U')
where gender not in ('M', 'F');
從這個問題,我認爲你有兩種情況。
NEW_VALUE
列數據爲空:使用COALESCE
UPDATE DWCUST DW
SET GENDER = (
SELECT coalesce(NEW_VALUE, 'U')
FROM GENDERSPELLING GS
WHERE DW.GENDER = GS.INVALID_VALUE
)
WHERE GENDER NOT IN (
'M'
,'F'
);
CORRELATED sub-Query
無返回值:使用擴展CASE
UPDATE DWCUST DW
SET GENDER = CASE
WHEN (
SELECT NEW_VALUE
FROM GENDERSPELLING GS
WHERE DW.GENDER = GS.INVALID_VALUE
) IS NULL
THEN 'U'
ELSE (
SELECT NEW_VALUE
FROM GENDERSPELLING GS
WHERE DW.GENDER = GS.INVALID_VALUE
)
END
WHERE GENDER NOT IN (
'M'
,'F'
);
OR COALESCE
外:
UPDATE DWCUST DW
SET GENDER = coalesce((
SELECT NEW_VALUE
FROM GENDERSPELLING GS
WHERE DW.GENDER = GS.INVALID_VALUE
), 'U')
WHERE GENDER NOT IN (
'M'
,'F'
);
你的意思'NEW_VALUE'場是'null'或沒有相應的行''GENDERSPELLING存在? – zarruq
select decode(new_value,null,'U',new_value)... – Frank
沒有對應的行存在@zarruq –