动态更新文件名mssql

时间:2018-01-25 10:27:01

标签: sql-server sql-server-2008 sql-server-2012

create table #sample(id varchar(100), set_type varchar(100) ,[group] int, Name varchar(100))
insert into #sample(id,set_type,[group])
select '12','Red',1
union all 
select '2346','Red',1
union all 
select '1235','Green',1
union all 
select '12890','Green',1
union all 
select '1208','Green',1
union all 
select '1234','Green',1
union all 
select '908472','Green',2
union all 
select '7958326','Blue',1

我需要动态更新表#sample的'名称'列,以便我得到低于所需的结果

Desire result : 

insert into #sample(id,set_type,[group],Name)
select '12','Red',1,'red_1_2_25012018'
union all 
select '2346','Red',1,'red_1_2_25012018'
union all 
select '1235','Green',1,'Green_1_2_25012018'
union all 
select '12890','Green',1,'Green_1_2_25012018'
union all 
select '1208','Green',1,'Green_3_4_25012018'
union all 
select '1234','Green',1,'Green_3_4_25012018'
union all 
select '908472','Green',2,'Green_5_5_25012018'
union all 
select '7958326','Blue',1,'Blue_1_1_25012018'

更新“名称”列的逻辑:

  1. 2 ID应该有一个名称,例如:ID = 12& 2346的“名称”= red_1_2_25012018,
  2. 取set_type = Green:'Name'= Green_1_2_25012018(Id:1235& 12890)和'Name'= Green_3_4_25012018(ID:1208& 1234)

    但ID:908472单独留下1 记录所以'Name'= Green_5_5_25012018 Blue只有1条记录 'name'= Blue_1_1_25012018

    1. 25012018 - DATEMONTHYEAR
    2. 所以,我想在cte ext之类的集合中执行此UPDATE'name'列,因为我必须更新超过数百万条记录。

0 个答案:

没有答案