问题的现象:
一个存储过程在删除表a的数据后,再向表a添加数据;同时又进行bcp导出视图,视图和表a相关。此时,存储过程一致无法执行完毕。30分钟后我的应用程序超时,自己重启了。发现表a的数据只插入了一部分。感觉应该是MSSQL死锁了,但不知道死算的原因是什么?请哪位大虾指点一下。万分感谢!

解决方案 »

  1.   

    存储过程:
    begin
        
        delete from tbl_A
        if(@@error <> 0)
        begin
            return -4      --出错
        end
        insert into tbl_A select * from tbl_B
        if(@@error<>0)
        begin
            return -4
        end
        
        return 0
    end另一个bcp存储过程:
     declare  @iRet   int            
        set @str='bcp "Bam..'+  @sTableNameTemp+' " ' 
             + ' out '
             + '"' +@sExportTableName + '" '
             +' -c  -q '
             + ' -U' + @psuser
             +' -P"' + @pspass
             +'"'    --执行bcp命令
        exec @iRet = master..xp_cmdshell @str
        
        if @iRet = 0     
            return 0
        else
            return -2     -- 数据库操作失败
      

  2.   

    存储过程: 
    begin 
        
        delete from tbl_A 
        if(@@error <> 0) 
        begin 
            return -4      --出错 
        end 
        insert into tbl_A(加锁) select * from tbl_B(nolock) 
        if(@@error <>0) 
        begin 
            return -4 
        end 
        
        return 0 
    end 另一个bcp存储过程: 
    declare  @iRet  int            
        set @str='bcp "Bam..'+  @sTableNameTemp+' " ' 
            + ' out ' 
            + '"' +@sExportTableName + '" ' 
            +' -c  -q ' 
            + ' -U' + @psuser 
            +' -P"' + @pspass 
            +'"'     --执行bcp命令 
        exec @iRet = master..xp_cmdshell @str 
        
        if @iRet = 0    
            return 0 
        else 
            return -2    -- 数据库操作失败
      

  3.   

    insert into tbl_A(加锁) select * from tbl_B(nolock) 能说明一下问题的根因到底是什么吗?
    这个问题很难重现,两个存储过程执行的时间都不长。
      

  4.   

    insert into tbl_A(加锁) select * from tbl_B(nolock) 向表插入数据的时候本来就应该有锁吧?