我使用以下语句存储过程。
Create table #pokedex
( tier int,
idNo int,
name varchar(50),
lastbattle datetime );
insert into #pokedex (idNO, name, lastbattle)
(select A.idNO, A.name, B.lastbattle
from differenttable A -- this table has data pulled from SAP
inner join alternatetable B -- this table has values inserted from SQLBULKCOPY
on A.idNo = B.idNo)
update #pokedex set tier = 3;
update #pokedex
set tier = 2
where idNO in (
select idNO from anothertable)
and tier = 3
Update #pokedex
Set tier = 1
where idNo in (
select idNo from #pokedex where lastbattle is not null
and tier = 2);
select * from #pokedex
order by tier, lastbattle;
结果
tier idNO name lastbattle
1 001 pidg null
1 002 ratt 2014-08-22 12:00
1 003 pika 2014-08-22 12:00
2 004 star null
2 005 muk null
2 004 mew null
我的问题:当我的更新条件为lastbattle not null时,为什么第一行有tier = 1
。
旁边信息:insert语句抓取由SQLBULKCOPY
命令填充的数据,其中用户上传了一个大型Excel文件。
请原谅我对口袋妖怪的爱,作为虚拟数据的替代品。
SOLVED - REPLACED UPDATE STATEMENT WITH
UPDATE #pokedex
SET TIER = 1
WHERE LASTBATTLE IS NOT NULL
AND TIER = 2;