将 Table-Value 函数从多行更改为内联

Change the Table-Value Function to Inline from Multi-line

我正在尝试更改 Table 值函数来提高存储过程的性能,但我无法获得相同的结果。你能告诉我哪里错了吗?

原文:

create FUNCTION [dbo].[fun_eess1] (
@institucion int ,
@disa int,
@red int,
@mred int) RETURNS @tbl TABLE (idlista [int] IDENTITY, institucion [int], disa [int], red [int], mred [int], establecimiento varchar(10)) AS BEGIN
IF (@institucion=1) -- First Condition */ where @institucion=1 then UNION ALL
BEGIN
    INSERT INTO @tbl ([institucion], [disa], [red], [mred], [establecimiento])
    SELECT DISTINCT [c_institucion], [c_disa], [c_red], [c_microred], [codrenae] FROM [dbo].[RENAE]
    WHERE 

    [c_institucion]=@institucion AND [c_disa]=(CASE @disa WHEN -1 THEN [c_disa] ELSE @disa END)
    AND [c_red]=(CASE @red WHEN -1 THEN [c_red] ELSE @red END)
    AND [c_microred]=(CASE @mred WHEN -1 THEN [c_microred] ELSE @mred END)
END  ELSE
BEGIN
IF (@institucion=15) -- Second Condition */ where @institucion=15 then UNION ALL
BEGIN
    INSERT INTO @tbl ([institucion], [disa], [red], [mred], [establecimiento])
    SELECT DISTINCT [c_institucion], [c_disa], [c_red], [c_microred], [codrenae] FROM [dbo].[RENAE]
    WHERE [c_institucion]='1' AND [c_disa]=(CASE @disa WHEN -1 THEN [c_disa] ELSE @disa END)
    AND [c_red]=(CASE @red WHEN -1 THEN [c_red] ELSE @red END)
    AND [c_microred]=(CASE @mred WHEN -1 THEN [c_microred] ELSE @mred END)

    INSERT INTO @tbl ([institucion], [disa], [red], [mred], [establecimiento])
    SELECT [org].[CodigoOrganizacion], [di].[iddisa], [r].[idred], [mi].[idmred], CAST ([es].[idestablecimiento] AS varchar) FROM [dbo].[Establecimientos] es
    INNER JOIN [dbo].[microred] mi ON [mi].[idmred]=[es].[microred]
    INNER JOIN [dbo].[Red] r ON [mi].[red] = [r].[idred]
    INNER JOIN [dbo].[Disa] di ON [r].[disa] = [di].[iddisa]
    INNER JOIN [dbo].[Organizacion] org ON [di].[institucion] = [org].[CodigoOrganizacion]
    WHERE [org].[CodigoOrganizacion] in ('2','3','4','5','6','13') AND [di].[iddisa]=(case @disa when -1 then [iddisa] else @disa end) AND [r].[idred]=(case @red when -1 then [idred] else @red end)  AND [mi].[idmred]=(case @mred when -1 then [idmred] else @mred end)
END
ELSE
BEGIN  -- Third Condition? */ where @institucion is not (1,15) then UNION ALL
    INSERT INTO @tbl ([institucion], [disa], [red], [mred], [establecimiento])
    SELECT [org].[CodigoOrganizacion], [di].[iddisa], [r].[idred], [mi].[idmred], CAST ([es].[idestablecimiento] AS varchar) FROM [dbo].[Establecimientos] es
    INNER JOIN [dbo].[microred] mi ON [mi].[idmred]=[es].[microred]
    INNER JOIN [dbo].[Red] r ON [mi].[red] = [r].[idred]
    INNER JOIN [dbo].[Disa] di ON [r].[disa] = [di].[iddisa]
    INNER JOIN [dbo].[Organizacion] org ON [di].[institucion] = [org].[CodigoOrganizacion]
    WHERE [org].[CodigoOrganizacion]=@institucion AND [di].[iddisa]=(case @disa when -1 then [iddisa] else @disa end) AND [r].[idred]=(case @red when -1 then [idred] else @red end)  AND [mi].[idmred]=(case @mred when -1 then [idmred] else @mred end) 
END END

希望有人能帮助我!

您可以通过使用 union all 创建一个大查询并将条件从 if 表达式移动到内部查询的 where 子句来更改它。此外,不是 return table 中的 Identity 列,而是将整个 union all 包装在另一个查询中,并且 select row_number over(order by @@spid)(returns 是一个任意的原始数字,因为它是按常量排序的):

CREATE FUNCTION [dbo].[fun_eess1] 
(
    @institucion int ,
    @disa int,
    @red int,
    @mred int
)

RETURNS TABLE 

AS BEGIN

    SELECT ROW_NUMBER() OVER(ORDER BY @@SPID) As idlista, *
    FROM (
        SELECT DISTINCT 
                [c_institucion] As [institucion], 
                [c_disa] As [disa], 
                [c_red] AS [red], 
                [c_microred] AS [mred], 
                [codrenae] AS [establecimiento]
        FROM [dbo].[RENAE]
        WHERE @institucion=1 -- First condition
        AND [c_institucion]=@institucion 
        AND [c_disa] = CASE @disa WHEN -1 THEN [c_disa] ELSE @disa END
        AND [c_red] = CASE @red WHEN -1 THEN [c_red] ELSE @red END
        AND [c_microred] = CASE @mred WHEN -1 THEN [c_microred] ELSE @mred END

        UNION ALL

        SELECT DISTINCT [c_institucion], [c_disa], [c_red], [c_microred], [codrenae] 
        FROM [dbo].[RENAE]
        WHERE @institucion=15 -- Second condition
        AND [c_institucion]='1' 
        AND [c_disa] = CASE @disa WHEN -1 THEN [c_disa] ELSE @disa END
        AND [c_red] = CASE @red WHEN -1 THEN [c_red] ELSE @red END
        AND [c_microred] = CASE @mred WHEN -1 THEN [c_microred] ELSE @mred END

        UNION ALL

        SELECT [org].[CodigoOrganizacion], [di].[iddisa], [r].[idred], [mi].[idmred], CAST ([es].[idestablecimiento] AS varchar) 
        FROM [dbo].[Establecimientos] es
        INNER JOIN [dbo].[microred] mi ON [mi].[idmred]=[es].[microred]
        INNER JOIN [dbo].[Red] r ON [mi].[red] = [r].[idred]
        INNER JOIN [dbo].[Disa] di ON [r].[disa] = [di].[iddisa]
        INNER JOIN [dbo].[Organizacion] org ON [di].[institucion] = [org].[CodigoOrganizacion]
        WHERE 
        (
            (
                @institucion=15 -- Second condition
                AND [org].[CodigoOrganizacion] in ('2','3','4','5','6','13')
            )
            OR 
            (
                @institucion NOT IN(1, 15) -- Third condition
                AND [org].[CodigoOrganizacion] = @institucion
            )
        )
        AND [di].[iddisa] = case @disa when -1 then [iddisa] else @disa end  
        AND [r].[idred] = case @red when -1 then [idred] else @red end 
        AND [mi].[idmred]= case @mred when -1 then [idmred] else @mred end

    )

END