Sql 具有嵌套 CASES 的标量值函数

Sql Scalar valued Function with nested CASES

我需要帮助来更正下面的语法,嵌套案例包含函数返回值到在另一个案例中评估的变量。

    CREATE FUNCTION dbo.EpsClassificacaoConjunto (@Bloco varchar(5), @Sub varchar(5), @SSub varchar(5))
RETURNS varchar(5)
  AS
  BEGIN
    DECLARE @Comprimento float = CASE WHEN @Sub is null AND @SSub not null then SELECT dbo.EpsComprimentoSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET'
                                      WHEN @SSub is null and @Sub not null then SELECT dbo.EpsComprimentoSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET'
                                      ELSE SELECT dbo.EpsComprimentoSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET' END;

    DECLARE @Largura float = CASE WHEN @Sub is null AND @SSub not null then SELECT dbo.EpsLarguraSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET'
                                  WHEN @SSub is null and @Sub not null then SELECT dbo.EpsLarguraSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET'
                                  ELSE SELECT dbo.EpsLarguraSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET' END;

    DECLARE @Altura float = CASE WHEN @Sub is null AND @SSub not null then SELECT dbo.EpsAlturaSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET'
                                 WHEN @SSub is null and @Sub not null then SELECT dbo.EpsAlturaSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET'
                                 ELSE SELECT dbo.EpsAlturaSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET' END;


    DECLARE @Peso float = CASE WHEN @Sub is null and @SSub is not null then SELECT Peso FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET'
                               WHEN @Sub not null and @SSub is null then SELECT Peso FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET'
                               ELSE SELECT Peso FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET' END;

    DECLARE @Pagamento varchar(2) = CASE WHEN @Sub is null and @SSub not null then SELECT TOP 1 CodigoDeFabricacao FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET'
                                         WHEN @Sub not null and @SSub is null then SELECT TOP 1 CodigoDeFabricacao FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET'
                                         ELSE SELECT TOP 1 CodigoDeFabricacao FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET' END;

    DECLARE @Fabricacao varchar(2) = CASE WHEN @Sub is null and @SSub not null then SELECT TOP 1 CodigoDeFabricacao FROM VShiPartBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET'
                                          WHEN @Sub not null and @SSub is null then SELECT TOP 1 CodigoDeFabricacao FROM VShiPartBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET'
                                          ELSE SELECT TOP 1 CodigoDeFabricacao FROM VShiPartBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET' END;

    RETURN CASE WHEN @Peso<5000 AND @Largura<2500 AND @Altura<4000 AND @Comprimento<10000 THEN '1-B'
                WHEN @Peso<5000 AND @Largura<5000 AND @Altura<4000 AND @Comprimento<10000 THEN '1-A'
                WHEN @Peso<10000 AND @Largura<10000 AND @Altura<4000 AND @Comprimento<10000 THEN '2'
                WHEN @Peso<5000 AND @Largura<2500 AND @Altura<4000 AND @Comprimento>=10000 AND @Comprimento<15000 THEN '3'
                ELSE 'ESP' END

  END
GO

逻辑部分已经描述过了,但是,看来我在使用 sql 语法时遇到了问题,请指教...

我在 MS SQL Server Management Studio 中对其进行了调试,并清楚地得到了这个 运行:

    CREATE FUNCTION EpsClassificacaoConjunto 
(
    -- Add the parameters for the function here
    @Bloco varchar(5),
    @Sub varchar(5),
    @SSub varchar(5)
)
RETURNS varchar(5)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @Result varchar(5)
    DECLARE @Comprimento float;
    DECLARE @Largura float;
    DECLARE @Altura float;
    DECLARE @Peso float;
    DECLARE @Pagamento varchar(2);
    DECLARE @Fabricacao varchar(2);

    -- Add the T-SQL statements to compute the return value here
    SELECT @Comprimento =  CASE WHEN (@Sub is null AND @SSub is not null) then (SELECT dbo.EpsComprimentoSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET')
                                WHEN (@SSub is null and @Sub is not null) then (SELECT dbo.EpsComprimentoSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET')
                                ELSE (SELECT dbo.EpsComprimentoSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET') END;

    SELECT @Largura = CASE WHEN (@Sub is null AND @SSub is not null) then (SELECT dbo.EpsLarguraSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET')
                            WHEN (@SSub is null and @Sub is not null) then (SELECT dbo.EpsLarguraSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET')
                            ELSE (SELECT dbo.EpsLarguraSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET') END;


      SELECT @Altura = CASE WHEN (@Sub is null AND @SSub is not null) then (SELECT dbo.EpsAlturaSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET')
                                 WHEN (@SSub is null and @Sub is not null) then (SELECT dbo.EpsAlturaSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET')
                                 ELSE (SELECT dbo.EpsAlturaSub(DimensaoDePerfil) FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET') END;



      SELECT @Peso = CASE WHEN (@Sub is null and @SSub is not null) then (SELECT Peso FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET')
                               WHEN (@Sub is not null and @SSub is null) then (SELECT Peso FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET')
                               ELSE (SELECT Peso FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET') END;


      SELECT @Pagamento = CASE WHEN (@Sub is null and @SSub is not null) then (SELECT TOP 1 CodigoDeFabricacao FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET')
                                         WHEN (@Sub is not null and @SSub is null) then (SELECT TOP 1 CodigoDeFabricacao FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET')
                                         ELSE (SELECT TOP 1 CodigoDeFabricacao FROM VShiAssemblyBom WHERE Bloco=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET') END;


      SELECT @Fabricacao = CASE WHEN (@Sub is null and @SSub is not null) then (SELECT TOP 1 CodigoDeFabricacao FROM VShiPartBom WHERE [Bloco Orig.]=@Bloco AND Submontagem is null AND Subsubmontagem=@SSub AND Tipo='SET')
                                          WHEN (@Sub is not null and @SSub is null) then (SELECT TOP 1 CodigoDeFabricacao FROM VShiPartBom WHERE [Bloco Orig.]=@Bloco AND Submontagem=@Sub AND Subsubmontagem is null AND Tipo='SET')
                                          ELSE (SELECT TOP 1 CodigoDeFabricacao FROM VShiPartBom WHERE [Bloco Orig.]=@Bloco AND Submontagem=@Sub AND Subsubmontagem=@SSub AND Tipo='SET') END;

    SELECT @Result = CASE WHEN @Peso<5000 AND @Largura<2500 AND @Altura<4000 AND @Comprimento<10000 THEN '1-B'
                            WHEN @Peso<5000 AND @Largura<5000 AND @Altura<4000 AND @Comprimento<10000 THEN '1-A'
                            WHEN @Peso<10000 AND @Largura<10000 AND @Altura<4000 AND @Comprimento<10000 THEN '2'
                            WHEN @Peso<5000 AND @Largura<2500 AND @Altura<4000 AND @Comprimento>=10000 AND @Comprimento<15000 THEN '3'
                            ELSE 'ESP' END

    -- Return the result of the function
    RETURN @Result

END
GO