【问题标题】:Composite update statement in SQLSQL 中的复合更新语句
【发布时间】: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


【解决方案1】:

你可以使用case;

update  a
set     
column_835 = (CASE WHEN b.color='blue' THEN (b.percent) Else column_835 End),
column_837 = (CASE WHEN b.color='green' THEN (b.percent) Else column_837 End),
column_842 = (CASE WHEN b.color='white' THEN (b.percent) Else column_842 End)
from    #esp a
join    #local_data b on a.c_no = b.c and a.fyear = b.fyear

【讨论】:

  • 这看起来很棒,不知道为什么想不出一个案例陈述。立即实施。
  • 我的结果集有 5 行连接匹配,但 column_835 仅在其中一行中更新。我需要它来更新每一行。我不确定为什么它不起作用
  • 你应该删除'blue'的where子句。
  • 你是什么意思。取出 WHEN 部分?很抱歉造成混乱
  • 我的意思是,您的原始查询有“where b.color = 'blue'”。你删除了吗?
【解决方案2】:
update  a
set     column_835 = (select b.percent from #local_data b where color ='blue' and b.fyear = a.fyear and b.c = a.c_no),
        column__837 = (select b.percent  from #local_data b where color= 'green' and b.fyear = a.fyear and b.c = a.c_no),
        column_y_842 = (select b.percent  from #local_data b where color= 'white' and b.fyear = a.fyear and b.c = a.c_no)
from    #esp a

【讨论】:

    【解决方案3】:

    如果@Colour 是输入参数,则尝试跟随

        DECLARE @id NVARCHAR(10) 
        SELECT
            @id = CAST(Id AS NVARCHAR(10))
        FROM
            T_VALUES v
        WHERE
            v.column_id = 728
        AND
            key_value = @Colour
    
    --Dynamic SQL part
    DECLARE @SQL NVARCHAR(MAX)
    
    SET @SQL = 
    '
        update  a
        set     column_' + @id + ' = b.percent
        from    #esp a
        join    #local_data b on a.c_no = b.c and a.fyear = b.fyear
        where   b.color = ''' + @Colour + '''
    ' ;
    --PRINT @SQL
    EXEC sys.sp_executesql @SQL ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多