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
这个存储过程一直执行,保服务器超时错误,到底什么原因望指教..
添加 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语句。
在SQL中用游标?太影响性能了,如果数据量小还可以,如果数据量大最好不要用,影响SQL性能的其中就有游标。劝你可以在程序中搞定
SQL中尽量少使用游标,因为对性能影响比较大,楼主这个是个死循环,在while中没有添加break跳出,所以一直会不断的执行知道超时,另外SQL中可以设置超时时间,可以根据自己的数据量进行设置