Sql Scalar使用嵌套CASES评估函数

时间:2016-09-08 16:22:05

标签: sql sql-server-2008 case user-defined-functions

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

    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语法有问题,请指教......

1 个答案:

答案 0 :(得分:0)

我在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