首页 新闻 会员 周边

数据库问题急急急急急急急

0
悬赏园豆:10 [已解决问题] 解决于 2010-12-10 08:51

Declare @TalentId nvarchar(10),
    @JobTalentPoolId nvarchar(20)
  SET NOCOUNT ON;
  begin tran
  Declare JobCollentTalentId Cursor  for
  select TalentId,JobTalentPool.TalentPoolId from JobTalentPool where CreatorId=@FromUserId
  open JobCollentTalentId
  fetch  next from JobCollentTalentId into @TalentId,@JobTalentPoolId
  while @@FETCH_STATUS=0
  begin
    if(not exists(select * from JobTalentPool where CreatorId=@ToUserId and TalentId=@TalentId))
    begin
     update JobTalentPool set CreatorId=@ToUserId where JobTalentPool.TalentPoolId=@JobTalentPoolId
    end 
  end
  update Jobs set UserId=@ToUserId where UserId=@FromUserId
  delete  from CompanyUsers where CompanyId=@CompanyId and PositionId in (select PositionId From Positions where UserId=@FromUserId)
  if(@@ERROR>0)
  begin
   RAISERROR(50001, 16, 1, '删除出错,请重试!')
   rollBack
   return -1
  end
  commit tran
  return 0

这个存储过程一直执行,保服务器超时错误,到底什么原因望指教..

MingHao_Hu的主页 MingHao_Hu | 初学一级 | 园豆:8
提问于:2010-11-02 15:12
< >
分享
最佳答案
0

添加 fetch  next from JobCollentTalentId into @TalentId,@JobTalentPoolId语句在while语句内部,如下:

fetch  next from JobCollentTalentId into @TalentId,@JobTalentPoolId
  while @@FETCH_STATUS=0
  begin
    if(not exists(select * from JobTalentPool where CreatorId=@ToUserId and TalentId=@TalentId))
    begin
     update JobTalentPool set CreatorId=@ToUserId where JobTalentPool.TalentPoolId=@JobTalentPoolId
     end 

   fetch  next from JobCollentTalentId into @TalentId,@JobTalentPoolId
  end

另外,在使用完游标后应调用CLOSE 和 DEALLOCATE语句。

收获园豆:10
项少龙 | 初学一级 |园豆:125 | 2010-11-02 17:59
其他回答(2)
0

在SQL中用游标?太影响性能了,如果数据量小还可以,如果数据量大最好不要用,影响SQL性能的其中就有游标。劝你可以在程序中搞定

天神一 | 园豆:845 (小虾三级) | 2010-11-02 18:45
0

SQL中尽量少使用游标,因为对性能影响比较大,楼主这个是个死循环,在while中没有添加break跳出,所以一直会不断的执行知道超时,另外SQL中可以设置超时时间,可以根据自己的数据量进行设置

雪莱·亨尼格 | 园豆:524 (小虾三级) | 2010-11-08 10:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册