-
SQL Server -- 解决存储过程传入参数作为sql语句条件值时,执行阻塞问题
成本核算程序执行某个存储过程一直阻塞,排查发现类似以下语句阻塞:
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
同样的程序、存储过程其它工厂核算时没有问题,即其它数据库没有出现阻塞,只在这个数据库阻塞,可能跟数据库设置有关,原理需要再研究。
出处:https://www.cnblogs.com/han-zhonggong/p/16696517.html
最新更新
谷歌、微软、Meta?谁才是 Python 最大的金
VB.NET编程调用讯雷下载文件
Objective-C语法之代码块(block)的使用
URL Encode
python爬虫学习
python爬虫学习——列表
go语言写http踩得坑
【Python】爬虫笔记-从PyMySQL到DBUtils
【Python】爬虫笔记-开源代理池haipproxy使用
Python规范:提高可读性
一招教你如何高效批量导入与更新数据
SQL Server -- 解决存储过程传入参数作为s
[SQL Server]按照设定的周别的第一天算任意
Linux下定时自动备份Docker中所有SqlServer数
链接服务器读取Mysql---出现消息 7347,级别
SQL高级(事务和触发器)
详解数仓的锁相关参数及视图
9个SQL运维常遇到的问题
sql server相关学习sql语句
读SQL进阶教程笔记05_关联子查询
JavaScript 中 Object,Map,Set 及数组遍历方法
微信小程序的全局弹窗以及全局实例
理解JS函数之call,apply,bind
解决未知的服务器标记“asp:ListView”。
css样式显示省略号
浅谈JS词法环境
js对象的理解
原型和原型链的深入浅出
JavaScript实现数组对象去重
关于 NodeJs 处理超长字符串问题的分析