插入语句作为存储过程的一部分,不插入行

时间:2017-06-01 19:49:45

标签: sql sql-server sql-server-2012

我不能为我的生活找出我在这个存储过程中创建的插入语句出错的地方。如果我在声明变量的过程之外运行它们,则会插入行而不会出现问题。我假设它是一个我忽略的简单语法问题。

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 = (@SixtyMinCount-@SixMinCount)/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

2 个答案:

答案 0 :(得分:2)

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

如果您要插入行return 1;而不是继续下一部分,请在return 1;之后移动insert

答案 1 :(得分:2)

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