代码之家  ›  专栏  ›  技术社区  ›  Chris Simpson

如果找不到结果,则在查询结果中添加空行

  •  20
  • Chris Simpson  · 技术社区  · 14 年前

    我正在编写由遗留系统调用的存储过程。遗留系统的限制之一是,从存储过程返回的单个结果集中必须至少有一行。标准是在第一列返回一个零(是的,我知道!)。

    另一种方法可能是在执行主查询之前对主查询中的同一where子句执行EXISTS。

    这两个都不是很令人满意。谁能想出更好的办法。我在想一个类似这样的联盟(我知道这行不通):

    --create table #test
    --(
    --  id int identity,
    --  category varchar(10)
    --)
    --go
    --insert #test values ('A')
    --insert #test values ('B')
    --insert #test values ('C')
    
    declare @category varchar(10)
    
    set @category = 'D'
    
    select
        id, category
    from #test
    where category = @category
    union
    select
        0, ''
    from #test
    where @@rowcount = 0
    
    6 回复  |  直到 14 年前
        1
  •  16
  •   Chris Simpson    8 年前

    这是个老问题,但我也有同样的问题。 解决方案非常简单,无需双重选择:

    select top(1) WITH TIES * FROM (
    select
    id, category, 1 as orderdummy
    from #test
    where category = @category
    union select 0, '', 2) ORDER BY orderdummy
    

    通过“WITH TIES”可以得到所有行(所有行都有一个1作为“orderdummy”,因此所有行都是TIES),或者如果没有结果,就得到defaultrow。

        2
  •  31
  •   Dave Newton    13 年前

    你总是要碰表两次,不管是COUNT,EXISTS before,EXISTS in UNION,TOP子句等等

    select
        id, category
    from mytable
    where category = @category
    union all --edit, of course it's quicker
    select
        0, ''
    where NOT EXISTS (SELECT * FROM mytable where category = @category)
    

    EXISTS解决方案比COUNT更好,因为它在找到行时将停止。COUNT将遍历所有行以实际计算它们

        3
  •  4
  •   Andrew Jansen    13 年前

    可以使用完全外部联接。一些影响。。。

    declare @category varchar(10)
    
    set @category = 'D'
    
    select #test.id, ISNULL(#test.category, @category) as category from (
        select
            id, category
        from #test
        where category = @category
    )  
    FULL OUTER JOIN (Select @category as CategoryHelper ) as EmptyHelper on 1=1   
    

        4
  •  3
  •   Robert Synoradzki    8 年前

    这是@swe的答案,只是格式更好。

    CREATE FUNCTION [mail].[f_GetRecipients]
    (
        @MailContentCode VARCHAR(50)
    )
    RETURNS TABLE
    AS
    RETURN
    (
        SELECT TOP 1 WITH TIES -- returns all rows having highest priority found
            [To],
            CC,
            BCC
        FROM (
            SELECT
                [To],
                CC,
                BCC,
                1 AS Priority -- if no rows, priority 2 under UNION will get returned
            FROM mail.Recipients
            WHERE 1 = 1
                AND IsActive = 1
                AND MailContentCode = @MailContentCode
    
            UNION ALL
    
            SELECT
                *
            FROM (VALUES
                (N'system@company.com', NULL, NULL, 2),
                (N'author@company.com', NULL, NULL, 2)
            ) defaults([To], CC, BCC, Priority)
        ) emails
        ORDER BY Priority
    )
    
        5
  •  1
  •   AllenG    14 年前

    我想你可以试试:

    Declare @count int
    set @count = 0
    
    Begin
    Select @count = Count([Column])
    From //Your query
    
    if(@Count = 0) 
       select 0
    else //run your query
    

    缺点是您有效地运行了两次查询,缺点是您跳过了temp表。

        6
  •  1
  •   JieLi    10 年前

    为了避免重复选择查询,不如先用一个临时表来存储查询结果?如果temp表为空,则根据temp表返回默认行;如果temp表有结果,则返回temp?