set nocount on应用:VBA+ADO访问数据库提示对象关闭异常

电商仓库写一个打印更换鞋盒条形码标签的excel文件,其中某一个调用存储过程,在SQL跑没有异常;在excel就是得不到结果;

 仔细的查询了vba代码,发现代码没有异常;所以开始怀疑是存储过程本身的问题;
过程如下:

create procedure [dbo].[SelectViewForTp]
@biid varchar(20),
@inco varchar(20)
as
begin
exec p_SelectInsertTp @biid
declare @sku varchar(20)
declare @error int
set @sku = (select top 1 inco from SwapCodeForVis where biid = @biid and  inco like @inco and tqty> cqty+sqty)
set @error = 0
update SwapCodeForVis set sqty = sqty +1 where inco = @sku
set @error = @@error + @error
if @error = 0
 begin
 select inco as '商品编码',tyco as '货号',LEFT(inve.inse,LEN(inve.inse)-3) as '颜色',brin.brde as '品牌'
  ,inst as '唯品货号',prty.tyna as '产品分类',convert(int,(RIGHT(inco,2)+10)/0.2) as '鞋号',colo as '执行标准'
  ,inve.rema as '货号材质',CONVERT(date,current_timestamp-30,12) as '生产日期'
  from inve
  join brin on brin.bran=inve.bran
  join prty on prty.inty =inve.inty
  where inco =  @sku
 end
end

检查语句本身没有异常,但是在想到vba调用的时候由于过程第一个事件是调用另外的过程和Insert;而不是仅有的一个有值的查询;

excel vba在这样调用的时候会出现异常;需要在过程开始处加上set nocount on,直到在查询语句之前加上 set nocount off;

这样当vba调用时直接调用到查询的结果集;查询结果正常:

--Ocarina我是一只可爱的小尾巴
原文地址:https://www.cnblogs.com/azrealer/p/11819672.html