成本核算程序执行某个存储过程一直阻塞,排查发现类似以下语句阻塞:
select tbl1.product_id,sum(isnull(tbl1.qty,0) * isnull(tbl2.unit_other_cost,0)) as other_cost from tbl1.p_id=@pId and tbl1.if_stock=0 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) group by tbl1.product_id
其中参数@pId是存储过程的传入参数。测试发现将条件中的@pId改成具体的值,直接执行SQL语句也会阻塞,但是加上变量定义就不会了:
declare @newpId int set @newpId=99 select tbl1.product_id,sum(isnull(tbl1.qty,0) * isnull(tbl2.unit_other_cost,0)) as other_cost from tbl1.p_id=@newpId and tbl1.if_stock=0 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) group by tbl1.product_id
最终决定存储过程里重新定义个变量,赋值为传入参数,将重新定义的变量作为条件值,问题解决。
declare @newpId int set @newpId=@pId select tbl1.product_id,sum(isnull(tbl1.qty,0) * isnull(tbl2.unit_other_cost,0)) as other_cost from tbl1.p_id=@newpId and tbl1.if_stock=0 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) group by tbl1.product_id
同样的程序、存储过程其它工厂核算时没有问题,即其它数据库没有出现阻塞,只在这个数据库阻塞,可能跟数据库设置有关,原理需要再研究。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通