2017-06-01 61 views
1

我不能为我的生活找出我出错的地方,我已经创建了插入语句作为此存储过程的一部分。如果我在声明变量的过程之外运行它们,它们插入的行没有问题。我假设它是一个我忽略的简单语法问题。插入语句作为存储过程的一部分,不插入行

BEGIN 
SET NOCOUNT ON; 

declare @SixMinCount int = 0 
, @SixtyMinCount int = 0 
, @Change float = 0.0 
, @PercentThreshold float = 0.07 
, @CountThreshold int = 900 
, @QualityLogNote varchar(500) = '' 

select @SixMinCount = COUNT(0) 
from dbo.Company c 
join dbo.CollectionSite cs with (nolock) 
on cs.CompanyId = c.CompanyId 
join dbo.SystemCode sc with (nolock) 
on sc.SystemCodeId = c.CompanyStateCd 
AND sc.SystemCodeTypeId = 12 
where c.ActiveInd = 'Y' --only active sites 
AND c.ApprovedInd = 'Y' --only approved sites 
AND cs.OwningNetworkId = 32971 --fieldprint network 
AND cs.TestSiteInd = 'N' --non test sites 
AND (select MAX(convert(varchar,lsWorkstationRequest.CreatedDt,120)) from dbo.lsWorkstationRequest where lsWorkstationId = c.CompanyId) > DATEADD(MI, -6, GETDATE()) --has not connected within the last five minutes 
AND c.CompanyNm not like 'fp%' --corporate stations and some demo/test stations that aren't marked as such 

select @SixtyMinCount = COUNT(0) 
from dbo.Company c 
join dbo.CollectionSite cs with (nolock) 
on cs.CompanyId = c.CompanyId 
join dbo.SystemCode sc with (nolock) 
on sc.SystemCodeId = c.CompanyStateCd 
AND sc.SystemCodeTypeId = 12 
where c.ActiveInd = 'Y' --only active sites 
AND c.ApprovedInd = 'Y' --only approved sites 
AND cs.OwningNetworkId = 32971 --fieldprint network 
AND cs.TestSiteInd = 'N' --non test sites 
AND (select MAX(convert(varchar,lsWorkstationRequest.CreatedDt,120)) from dbo.lsWorkstationRequest where lsWorkstationId = c.CompanyId) > DATEADD(MI, -60, GETDATE()) --has not connected within the last five minutes 
AND c.CompanyNm not like 'fp%' --coporate stations and some demo/test stations that aren't marked as such 

IF (@SixMinCount < @CountThreshold) 
begin 
    return 1; -- 1 denotes 'something is wrong' 

     set @QualityLogNote = ('The total number of Sites connected is below the minimum threshold.' 
     + Char(10) + char(13) + 'Sites Connected Number Threshold = '+ convert(varchar, @CountThreshold) + Char(10)+ Char(13) + 
     'Sites Currently Connected = ' + Convert(varchar, @SixMinCount)) 

     insert into QualityLog (NonconformityCd, OccuranceDt, ActiveInd, DeliveredInd, CorrectedInd, Notes) 
     values ('FP-IT Site Alert', GetDate(), 'Y', 'N', 'N', @QualityLogNote) 
end 
--ELSE 
begin 
    select @Change = (@[email protected])/1337.0--.0 to force sql server to use float 



    if (@Change > @PercentThreshold) 
    begin 
     return 1; 

      set @QualityLogNote = ('There has rapid change in the number of sites connecting in the past 6 minutes' 
      + Char(10) + char(13) + 'Threshold for % change in number of sites connected = '+ convert(varchar, Cast(Cast((@PercentThreshold)*100 as decimal(18,2)) as varchar(5)) + '%') + Char(10)+ Char(13) + 
      'Percent change between cycles = ' + Convert(varchar, Cast(Cast((@Change)*100 as decimal(18,2)) as varchar(5)) + '%')) 

      insert into QualityLog (NonconformityCd, OccuranceDt, ActiveInd, DeliveredInd, CorrectedInd, Notes) 
      values ('FP-IT Site Alert', GetDate(), 'Y', 'N', 'N', @QualityLogNote) 
    end 
    else 
    begin 
     return 0; -- 0 denotes 'all is well' 
    end 
end 

END

+0

你应该仔细看看这个。 http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/ –

+0

@SeanLange我知道,但我是一个开发人员没有试图摇摇欲坠的人,我知道更多有经验的团队成员已经提出已经是主要开发者的问题了。 –

+0

呃我不羡慕那种情况。我已经看到主要开发者坚决反对在所有建议中反对的立场。它几乎总是以一个庞大的项目结束,经历一切并摆脱它。但这是另一天的讨论。 –

回答

2

当你return 1;,程序将停止在那里。

如果要插入行然后return 1;而不是继续到下一节,然后移动insert后的return 1;

+0

只是为了好玩,如果你对AI有兴趣,麻省理工学院已经发布了一些讲座http://www.openculture.com/2017/05/artificial-intelligence-a-free-online-course-from-mit.html –

+0

@JohnCappelletti整洁 – SqlZim

2

在INSERT语句之上注释掉返回1。返回命令会中断逻辑流程,所以RETURN之后的代码不会被执行。