【发布时间】:2018-01-10 17:13:21
【问题描述】:
我不知道该怎么做--
我有一个本地表,其中包含需要更新的字段。
select column_835 1,
column_836 = 6,
column_837 = 8,
column_838 = 1,
column_839 = 6,
column_840 = 3,
column_841 = 6,
column_842 = 8
from #esp
更新语句是这样写的 --
update a
set column_835 = b.percent
from #esp a
join #local_data b on a.c_no = b.c and a.fyear = b.fyear
where b.color = 'blue'
发生的情况是我想用我的本地表中颜色为蓝色的值更新行“column_835”。
第二个查询将使用百分比值更新“column_837”,但它对应于绿色等。我想做的是如何制作一个更新声明而不是 8 个或更多。 (如果添加了更多颜色,我们必须在代码中进行更多更新)。
我怎么说更新column_ from our local table with the
so for example
column_835 = percent (but only where b.color = blue)
column_837 = percent (but only where b.color = green)
column_842 = percent (but only where b.color = white)
and so on.
数据来自下面—— 我还有第二张表,里面有这样的数据
select *
from T_VALUES v
where v.column_id = 728
column_id 728 的表“values”中的数据返回如下
id keyword_no key_value
840 728 red
839 728 white
837 728 green
835 728 blue
841 728 yellow
838 728 black
842 728 white
836 728 purple
【问题讨论】:
-
为什么不使用带有动态 SQL 的存储过程
-
@Cool_Br33ze 我正在编写一个过程(即更新语句所在的内容)以及大部分代码。但列已预先确定,无法更改。我只是不确定如何编写一个更新语句来智能地更新所有行而不是 8 个更新语句。
标签: sql-server join sql-update multiple-columns