执行这个存储过程,OA_TAB_PHONECHARGE表就锁住了,是X锁,谁能帮我看看,解决了就给分. CREATE procedure Proc_updatePhoneCharge
as
if datepart(day,getdate()) =31
begin  declare @usercode char(10)
declare @monthcharge decimal
declare @sparecharge decimal
begin transaction
declare phonecharge cursor
for
SELECT pc_user_code,pc_month_charge,pc_spare_charge FROM DBO.OA_TAB_PHONECHARGE 
where pc_id in
(
SELECT max(pc_id) FROM DBO.OA_TAB_PHONECHARGE where PC_month_CHARGE>0 GROUP BY PC_USER_CODE
)
open phonecharge
        fetch phonecharge into @usercode,@monthcharge,@sparecharge
declare @err int
        select @err = 0
        begin transaction t1
while (@@fetch_status=0)
begin
if datepart(month,getdate()) = 1 
begin
insert into DBO.OA_TAB_PHONECHARGE(PC_USER_CODE,PC_MONTH_CHARGE,PC_SPARE_CHARGE,PC_CREATOR,PC_CREATEDATE)
values(@usercode,@monthcharge,@monthcharge,'200718',getdate())
select @err = @err + @@error

end
else
begin 
insert into DBO.OA_TAB_PHONECHARGE(PC_USER_CODE,PC_MONTH_CHARGE,PC_SPARE_CHARGE,PC_CREATOR,PC_CREATEDATE)
values(@usercode,@monthcharge,@sparecharge+@monthcharge,'200718',getdate())
select @err = @err + @@error
fetch next from phonecharge into @usercode,@monthcharge,@sparecharge
end 
end
        if @err = 0
commit transaction t1
        else
                rollback transaction t1
close phonecharge
deallocate  phonecharge
end
GO

解决方案 »

  1.   

    declare @usercode char(10)
    declare @monthcharge decimal
    declare @sparecharge decimal
    begin transaction----这个事务的提交和回滚处理呢?因为事务没有提交
    所以表被锁定了
      

  2.   

    if @err = 0
    commit transaction t1
            else
                    rollback transaction t1这不是回滚和提交吗?begin transaction 肯定是要的,我要保证insert全部成功或者全部失败。
      

  3.   

    导致死锁的错误是因为if datepart(month,getdate()) = 1时导致死循环.事务不匹配是另外一个错误,但不是导致死锁的原因.
    这样改一下:
    while (@@fetch_status=0)
    begin
        if datepart(month,getdate()) = 1 
        begin
            insert into ...
            select @err = @err + @@error
        end
        else
        begin 
            insert into ...
            select @err = @err + @@error
        end
        ----修改此处,将fetch next写到if..else..外面
        fetch next from phonecharge into @usercode,@monthcharge,@sparecharge
    end
      

  4.   

    我看去点头绪了
    fetch next from phonecharge into @usercode,@monthcharge,@sparecharge
    这行有问题,我试试
      

  5.   

    if @err = 0
    commit transaction t1
            else
                    rollback transaction t1这不是回滚和提交吗?
    这是提交,但是是对下面一个事务的提交啊,一开始那个呢?
    fetch phonecharge into @usercode,@monthcharge,@sparecharge
    declare @err int
            select @err = 0
            begin transaction t1
      

  6.   

    alter procedure Proc_updatePhoneCharge
    as
    if datepart(day,getdate()) =31
    begin  declare @usercode char(10)
    declare @monthcharge decimal
    declare @sparecharge decimal
    begin transaction
    declare phonecharge cursor
    for
    SELECT pc_user_code,pc_month_charge,pc_spare_charge FROM DBO.OA_TAB_PHONECHARGE 
    where pc_id in
    (
    SELECT max(pc_id) FROM DBO.OA_TAB_PHONECHARGE where PC_month_CHARGE>0 GROUP BY PC_USER_CODE
    )
    open phonecharge
            fetch phonecharge into @usercode,@monthcharge,@sparecharge
    declare @err int
            select @err = 0
            begin transaction t1
    while (@@fetch_status=0)
    begin
    if datepart(month,getdate()) = 1 
    begin
    insert into DBO.OA_TAB_PHONECHARGE(PC_USER_CODE,PC_MONTH_CHARGE,PC_SPARE_CHARGE,PC_CREATOR,PC_CREATEDATE)
    values(@usercode,@monthcharge,@monthcharge,'200718',getdate())
    select @err = @err + @@error
    end
    else
    begin 
    insert into DBO.OA_TAB_PHONECHARGE(PC_USER_CODE,PC_MONTH_CHARGE,PC_SPARE_CHARGE,PC_CREATOR,PC_CREATEDATE)
    values(@usercode,@monthcharge,@sparecharge+@monthcharge,'200718',getdate())
    select @err = @err + @@error

    end 
    fetch next from phonecharge into @usercode,@monthcharge,@sparecharge
    end
            if @err = 0
    commit transaction t1
            else
                    rollback transaction t1
    close phonecharge
    deallocate  phonecharge
    end
    execute Proc_updatePhoneCharge
      

  7.   

    把fetch next from phonecharge into @usercode,@monthcharge,@sparecharge移到下面来了还是不行。
    to hhhdyj()
    我不太会写存储过程,对你说的意思也不是很明白,你能帮我改改吗?
    我就是想开始一个事务,如果出错就回滚,如果执行到最后就提交整个事务。能帮帮我吗?谢谢了!
      

  8.   

    象一楼朋友说的那样,删除begin transaction.
      

  9.   

    CREATE procedure Proc_updatePhoneCharge
    as
    if datepart(day,getdate()) =31
    begin  declare @usercode char(10)
    declare @monthcharge decimal
    declare @sparecharge decimal
    ------- begin transaction----没有用,要是加上的话,还要多一个提交事务的语句
    declare phonecharge cursor
    for
    SELECT pc_user_code,pc_month_charge,pc_spare_charge FROM DBO.OA_TAB_PHONECHARGE 
    where pc_id in
    (
    SELECT max(pc_id) FROM DBO.OA_TAB_PHONECHARGE where PC_month_CHARGE>0 GROUP BY PC_USER_CODE
    )
    open phonecharge
            fetch phonecharge into @usercode,@monthcharge,@sparecharge
    declare @err int
            select @err = 0
            begin transaction t1
    while (@@fetch_status=0)
    begin
    if datepart(month,getdate()) = 1 
    begin
    insert into DBO.OA_TAB_PHONECHARGE(PC_USER_CODE,PC_MONTH_CHARGE,PC_SPARE_CHARGE,PC_CREATOR,PC_CREATEDATE)
    values(@usercode,@monthcharge,@monthcharge,'200718',getdate())
    select @err = @err + @@error

    end
    else
    begin 
    insert into DBO.OA_TAB_PHONECHARGE(PC_USER_CODE,PC_MONTH_CHARGE,PC_SPARE_CHARGE,PC_CREATOR,PC_CREATEDATE)
    values(@usercode,@monthcharge,@sparecharge+@monthcharge,'200718',getdate())
    select @err = @err + @@error
    end
    fetch next from phonecharge into @usercode,@monthcharge,@sparecharge
    end
            if @err = 0
    commit transaction t1
            else
                    rollback transaction t1
    close phonecharge
    deallocate  phonecharge
    end
    GO