SQL Server -- 解决存储过程传入参数作为sql语句条件值时,执行阻塞问题 ...

打印 上一主题 下一主题

主题 912|帖子 912|积分 2736

成本核算程序执行某个存储过程一直阻塞,排查发现类似以下语句阻塞:
  1. select
  2.     tbl1.product_id,sum(isnull(tbl1.qty,0) * isnull(tbl2.unit_other_cost,0)) as other_cost
  3. from tbl1.p_id=@pId and tbl1.if_stock=0
  4.     and exists(select 1 from tbl3 inner join tbl4 on tbl3.c1=tbl4.c2 where tbl4.c3=0 and tbl3.p_id=tbl1.p_id and tbl3.product_id=tbl1.product_id)
  5. group by tbl1.product_id
复制代码
其中参数@pId是存储过程的传入参数。测试发现将条件中的@pId改成具体的值,直接执行SQL语句也会阻塞,但是加上变量定义就不会了:
  1. declare @newpId int
  2. set @newpId=99
  3. select
  4.     tbl1.product_id,sum(isnull(tbl1.qty,0) * isnull(tbl2.unit_other_cost,0)) as other_cost
  5. from tbl1.p_id=@newpId and tbl1.if_stock=0
  6.     and exists(select 1 from tbl3 inner join tbl4 on tbl3.c1=tbl4.c2 where tbl4.c3=0 and tbl3.p_id=tbl1.p_id and tbl3.product_id=tbl1.product_id)
  7. group by tbl1.product_id
复制代码
最终决定存储过程里重新定义个变量,赋值为传入参数,将重新定义的变量作为条件值,问题解决。
  1. declare @newpId int
  2. set @newpId=@pId
  3. select
  4.     tbl1.product_id,sum(isnull(tbl1.qty,0) * isnull(tbl2.unit_other_cost,0)) as other_cost
  5. from tbl1.p_id=@newpId and tbl1.if_stock=0
  6.     and exists(select 1 from tbl3 inner join tbl4 on tbl3.c1=tbl4.c2 where tbl4.c3=0 and tbl3.p_id=tbl1.p_id and tbl3.product_id=tbl1.product_id)
  7. group by tbl1.product_id
复制代码
同样的程序、存储过程其它工厂核算时没有问题,即其它数据库没有出现阻塞,只在这个数据库阻塞,可能跟数据库设置有关,原理需要再研究。
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

西河刘卡车医

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表