Search code examples
sqlsql-serverssms

Is there a way to separate query results in SQL Server Management Studio (SSMS)?


I got a simple query which return a results from an OrderLine table. Is there a way to visually separate the query results to make it easier to read, like in the image shown here?

SELECT [OrderNo], [LineNo] 
FROM [OrderLine]

Results:

Query Results


Solution

  • drop table if exists #OrderLine;
    
    select object_id as OrderNo, abs(checksum(newid())) as [LineNo]
    into #OrderLine
    from sys.columns;
    
    -- ... results to text (ctrl+T)?
    select OrderNo, [LineNo], 
    case when lead(OrderNo, 1) over(partition by OrderNo order by OrderNo) = OrderNo then '' else replicate('-', 11) + char(10) end
    from #OrderLine;
    
    --inject NULL
    select case when [LineNo] is null and flag=2 then null else TheOrderNo end as OrderNo, [LineNo]
    from
    (
        select OrderNo AS TheOrderNo, [LineNo], 1 as flag
        from #OrderLine
        union all
        select distinct OrderNo, NULL, 2
        from #OrderLine
    ) as src
    order by TheOrderNo, flag;