我想使用case语句在一个表中更新一列但多行。 这是更新声明:
UPDATE UDSDiagnosisGroups
SET UDSDiagnosisCategoryID =
CASE WHEN LineDisplay IN ('1-2', '3', '4', '4a', '4b') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = 'Selected Infectious and Parasitic Diseases')
WHEN LineDisplay IN ('5', '6') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = 'Selected Diseases of the Respiratory System')
WHEN LineDisplay IN ('7', '8', '9', '10', '11', '12', '13', '14', '14a') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = 'Selected Other Medical Conditions')
WHEN LineDisplay IN ('15', '16', '17') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = ' Selected Childhood Conditions (limited to ages 0 through 17)')
WHEN LineDisplay IN ('18', '19', '19a', '20a', '20b', '20c', '20d') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = 'Selected Mental Health and Substance Abuse Conditions')
END
FROM UDSDiagnosisGroups
WHERE UDSDiagnosisCategoryID IS NULL
问题是UDSDiagnosisGroups
表中只更新了一行。
更新的行是LineDisplay
值1-2
的行。
所有其他值仍为NULL
。
我哪里错了?它在我看来,我正在按照我找到的示例,但只更新了一行,而不是表中的所有行。
答案 0 :(得分:0)
我建议将您的代码更改为类似的内容,它会更具可读性
array = array[0];
然后您可以播放case
而不是case
来查看UPDATE UDSDiagnosisGroups SET
UDSDiagnosisCategoryID = d.UDSDiagnosisCategoryID
from UDSDiagnosisGroups as g
inner join UDSDiagnosisCategories as d on
d.UDSDiagnosisCategory =
case
when g.LineDisplay in ('1-2', '3', '4', '4a', '4b') then
'Selected Infectious and Parasitic Diseases'
when g.LineDisplay in ('5', '6') then
'Selected Diseases of the Respiratory System'
when g.LineDisplay in ('7', '8', '9', '10', '11', '12', '13', '14', '14a') then
'Selected Other Medical Conditions'
when g.LineDisplay in ('15', '16', '17') then
' Selected Childhood Conditions (limited to ages 0 through 17)'
when g.LineDisplay in ('18', '19', '19a', '20a', '20b', '20c', '20d') then
'Selected Mental Health and Substance Abuse Conditions'
where
g.UDSDiagnosisCategoryID is null
返回的行是否有效。你可能会有一些错别字。没有数据就很难说清楚。
答案 1 :(得分:0)
可能是你的列值为空而不是空。使用 ISNULL 并重试
UPDATE UDSDiagnosisGroups
SET UDSDiagnosisCategoryID =
CASE WHEN LineDisplay IN ('1-2', '3', '4', '4a', '4b') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = 'Selected Infectious and Parasitic Diseases')
WHEN LineDisplay IN ('5', '6') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = 'Selected Diseases of the Respiratory System')
WHEN LineDisplay IN ('7', '8', '9', '10', '11', '12', '13', '14', '14a') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = 'Selected Other Medical Conditions')
WHEN LineDisplay IN ('15', '16', '17') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = ' Selected Childhood Conditions (limited to ages 0 through 17)')
WHEN LineDisplay IN ('18', '19', '19a', '20a', '20b', '20c', '20d') THEN (SELECT UDSDiagnosisCategoryID
FROM UDSDiagnosisCategories
WHERE UDSDiagnosisCategory = 'Selected Mental Health and Substance Abuse Conditions')
END
FROM UDSDiagnosisGroups
WHERE ISNULL(UDSDiagnosisCategoryID,'') = ''