我收到了一个电子表格,格式为:
Id | Val
1 57
2 99
大约有10,000条记录-在不手动编写每个case语句的情况下,处理以下查询10,000条记录的任何想法都很繁琐。谢谢。
update person
SET val = (
case
when Id = 1 then 57
when Id = 2 then 99
end),
where Id in (1, 2)
答案 0 :(得分:2)
又快又脏?在这里
添加一个新的spredsheet,调用旧的datatable
在第一行的第一列中编写
“更新人员集val =(”
在第二列中,您链接到数据表电子表格上的值
第三列“),其中ID =(”
您将第四列链接到数据表电子表格的ID
第五列“)”
然后标记整行并将其向下拉至10000行
复制过去到查询escecute
答案 1 :(得分:0)
我认为这个示例可以为您提供帮助:
CREATE TABLE #Person
(PrimaryKey int PRIMARY KEY,
ValueSome varchar(50)
);
GO
CREATE TABLE #MySpreadSheet
(PrimaryKey int PRIMARY KEY,
ValueSpread varchar(50)
);
GO
INSERT INTO #Person
SELECT 1, 'someValue'
INSERT INTO #Person
SELECT 2, 'someValueBeforeUpdate'
INSERT INTO #Person
SELECT 3, ''
INSERT INTO #MySpreadSheet
SELECT 1, '45'
INSERT INTO #MySpreadSheet
SELECT 2, '56'
INSERT INTO #MySpreadSheet
SELECT 3, '34'
SELECT * FROM #Person
SELECT * FROM #MySpreadSheet
UPDATE P SET P.ValueSome = SS.ValueSpread FROM #Person P JOIN #MySpreadSheet SS ON P.PrimaryKey = SS.PrimaryKey
SELECT * FROM #Person
DROP TABLE #Person
DROP TABLE #MySpreadSheet
答案 2 :(得分:0)
如果有人感兴趣,我会这样:
CREATE TABLE #TempTable(
Id int,
val int
)
INSERT INTO #TempTable (Id, val)
Values (1, 57),
(2, 99)
Update Person
Set Id = tp.Id,
val = tp.val
FROM Person p
INNER JOIN #TempTable as tp on tp.Id = p.Id
答案 3 :(得分:-1)
create table #example (id int , value int)
insert into #example (id, value) values (1, 10)
insert into #example (id, value) values (2, 20)
select * from #example
id value
1 10
2 20
update #example
set value = case when id = 1 then 100
when id = 2 then 200 end
where id in (1,2)
select * from #example
id value
1 100
2 200