日期:2014-05-18  浏览次数:20411 次

请大家帮我看看这个问题,太奇怪了。通过一张表更新另一张表某一字段的问题。
update a set a.student_syszddm=b.dwszddm from stuinfos_base a,Confer_dwszddm b where a.student_syszdmc =b.dwszd and a.student_year=' '

这是一条非常普通的SQL语句,不过我现在遇到一个非常稀奇的问题!
我需要使用 Confer_dwszddm(这张表有3000多条记录)里面的 dwszddm 字段的内容 去填充 stuinfos_base(这张表有11万条记录)里面的 student_syszddm 字段。前提条件是 两张表的 a.student_syszdmc =b.dwszd 字段要相同。
然后我再通过a.student_year=' '来确定年份。
我在匹配 2005届、2006届、2007届 ·····到2011届数据,更新执行都非常迅速。但是 2012届的数据,就出现问题了。老是提示我 超时。
我就纳闷了,都是一张表里面的数据,为什么,前几年的数据,我执行更新就没问题。偏偏这个最新的数据就出现这种 超时,没法匹配的情况。


请大家帮我分析一下。 


------解决方案--------------------
是不是有其他操作,锁住了 stuinfos_base 表,而锁住的那条记录,就是 2012 届的?
------解决方案--------------------
以下代码,模拟出一种可能的情况,
SQL code

-- 建测试表
create table stuinfos_base (id int, student_name varchar(10), student_year int)

-- 插入测试数据
insert into stuinfos_base
select 1,'Lix',2008 union all
select 2,'Sun',2009 union all
select 3,'Wei',2010 union all
select 4,'Jie',2011 union all
select 5,'Fan',2012

-- 建触发器
create trigger tr_stuinfosbase 
on stuinfos_base
after update
as
begin
 declare @year int
 select @year=student_year from inserted
 if @year=2012  -- 如果更新的是2012届的资料
 begin
   waitfor delay '00:00:10'  -- 延时10秒
 end
end

-- 更新2008届的资料
update stuinfos_base set student_name='Lix2' where student_year=2008
--> 执行很快

-- 更新2009届的资料
update stuinfos_base set student_name='Sun2' where student_year=2009
--> 执行很快

-- 更新2012届的资料,问题就来啦,因为进入了触发器,延时10秒.
update stuinfos_base set student_name='Fan2' where student_year=2012
--> 等10秒完成



-- 把2012届的数据独立出来
select * into stuinfos_base_2012 from stuinfos_base where student_year=2012

update stuinfos_base_2012 set student_name='Fan2' where student_year=2012
--> 执行很快,因为没有触发器.