微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQL 函数不返回表中的值,但单独运行时会返回

如何解决SQL 函数不返回表中的值,但单独运行时会返回

我有一个函数,由于某种原因没有将数据带回表中,但如果我减少代码并自行测试它,它就可以工作。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[ReportingCyclesForReporting]
(
    @ReportingCycleId BIGINT
)
RETURNS @t TABLE
(
    [ReportingCycleId] [bigint] NOT NULL,[BusinessId] [bigint] NOT NULL,[StartDate] [datetime] NOT NULL,[EndDate] [datetime] NOT NULL,[ScenarioId] [bigint] NULL,[SourceReportingCycleId] [bigint] NULL,[MjMEPerDay] [decimal](19,4) NOT NULL,[AssetAppreciation] [decimal](5,2) NULL,[LandAssetAppreciation] [decimal](5,[OffFarmAssetAppreciation] [decimal](5,[RolledOverDate] [datetime] NULL,[Version] [int] NOT NULL,[Archived] [bit] NOT NULL,[Essential] [bit] NOT NULL,[EssentialsLeasedLandValue] [decimal](19,[EssentialsShareFarmedLandValue] [decimal](19,[EssentialsWaterValue] [decimal](19,[RolloverDataOverwrite] [bit] NULL
)
AS
BEGIN
    DECLARE @businessId INT
    DECLARE @scenarioId INT
    DECLARE @essential BIT

    -- Get businessid and scenario id for this reporting cycle
    SELECT @businessId = BusinessId,@scenarioId = ScenarioId,@essential = Essential
      FROM ReportingCycle
     WHERE Id          = @ReportingCycleId
       AND Archived    = 0
       BEGIN
    IF @scenarioId IS NOT NULL
    begin
        -- Scenario reporting cycle siblings of the given reporting cycle
        INSERT INTO @t(
               ReportingCycleId,BusinessId,StartDate,EndDate,ScenarioId,SourceReportingCycleId,MjMEPerDay,AssetAppreciation,LandAssetAppreciation,OffFarmAssetAppreciation,RolledOverDate,Version,Archived,Essential,EssentialsLeasedLandValue,EssentialsShareFarmedLandValue,EssentialsWaterValue
        )
        SELECT rc.Id,rc.BusinessId,rc.StartDate,rc.EndDate,rc.ScenarioId,rc.sourceReportingCycleId,rc.MjMEPerDay,rc.AssetAppreciation,rc.LandAssetAppreciation,rc.OffFarmAssetAppreciation,rc.RolledOverDate,rc.Version,rc.Archived,rc.Essential,rc.EssentialsLeasedLandValue,rc.EssentialsShareFarmedLandValue,rc.EssentialsWaterValue
          FROM ReportingCycle
        INNER JOIN ReportingCycle rc ON rc.ScenarioId = ReportingCycle.ScenarioId AND rc.Archived = 0
         WHERE ReportingCycle.Id = @ReportingCycleId
           AND ReportingCycle.Archived = 0
        ORDER BY rc.StartDate
        end
    ELSE IF EXISTS(SELECT * FROM FutureReportingCycles(@businessId,@essential) WHERE Id = @reportingCycleId)
    begin
        -- Future reporting cycles
        INSERT INTO @t(
               ReportingCycleId,EssentialsWaterValue
        )
        SELECT Id,EssentialsWaterValue
        FROM FutureReportingCycles(@businessId,@essential)
        END
    ELSE IF EXISTS(SELECT * FROM HistoryReportingCycles(@businessId,@essential) WHERE Id = @reportingCycleId and ScenarioId is null)
    BEGIN
        -- History reporting cycles
        INSERT INTO @t(
               ReportingCycleId,EssentialsWaterValue
        FROM HistoryReportingCycles(@businessId,@essential)  where  ScenarioId is null
        END
    ELSE
    -- Now
    BEGIN
        INSERT INTO @t(
               ReportingCycleId,EssentialsWaterValue
        FROM ReportingCycle WHERE id = @ReportingCycleId and Archived = 0
    END

    END
    RETURN 

END

我会执行这个来触发它进行测试

  select * from ReportingCyclesForReporting(14212)

在未来的报告周期中会发生这种情况。我会得到一张空桌子。我已经在 Visual Studio 中对此进行了调试,我可以看到它以正确的值进入了正确的分支,但该表没有返回任何内容

但是,如果我运行脚本的精简版本,则会恢复这些值。

DECLARE @t TABLE
(
[ReportingCycleId] [bigint] NOT NULL,[RolloverDataOverwrite] [bit] NULL
)
    INSERT INTO @t(
           ReportingCycleId,EssentialsWaterValue
    )
    SELECT Id,EssentialsWaterValue
    FROM FutureReportingCycles(306,0)

    select * from @t

这将带回价值

enter image description here

这是它在调试模式下的图片显示它进入该分支。

我真的很感激这方面的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。