【发布时间】:2020-05-21 01:54:54
【问题描述】:
我有一长串:
update wi
SET class
case
WHEN wi.column = 'a' THEN (select top 1 from lookup_tbl1 where code = 1)
WHEN wi.column = 'b' THEN (select top 1 from lookup_tbl1 where code = 2)
WHEN wi.column = 'c' THEN (select top 1 from lookup_tbl1 where code = 3)
WHEN wi.column = 'd' THEN (select top 1 from lookup_tbl1 where code = 80)
end
SET name
case
WHEN wi.column2 = 'Chris' THEN (select top 1 from table1 where code = 1)
WHEN wi.column2 = 'david' THEN (select top 1 from table1 where code = 2)
WHEN wi.column2 = 'tan' THEN (select top 1 from table1 where code = 3)
WHEN wi.column2 = 'drake' THEN (select top 1 from table1 where code = 80)
WHEN wi.column2 = 'x' THEN ..........
end
SET department
case
WHEN wi.column3 like 'd.d%' then 'Director D'
WHEN wi.column3 like '%AC' then 'Accounting'
end
FROM transform_tbl wi WHERE flag is null
我正在清理 transform_tbl 表。我需要用实际名称替换缩写并修复该表中的数据。 还有其他方法或更好的方法吗?
【问题讨论】:
-
它实际上被称为 case 表达式——正是因为它不是一个语句。
-
您需要包含一些外部语句,以便我们可以看到列来自哪里以及涉及哪些其他表。
-
请显示完整的查询和数据结构
-
您的查询在语法上不正确,因为子查询需要选择某些内容。这也有助于查看查询的其余部分。并了解为什么您使用
top而不使用order by。 -
我在问题中添加了更多细节。 @DaleK
标签: sql sql-server tsql sql-update subquery