Insert dummy row in SQL select query

后端 未结 2 853
悲哀的现实
悲哀的现实 2021-01-13 02:43

I am working on SQL Server stored procedure. I have a table \'User\' having fields (Id, Name, Email,Address).

I have following query returning All Users

<         


        
相关标签:
2条回答
  • 2021-01-13 03:02
    SELECT 0 AS Id, 'All' AS Name
    UNION ALL
    SELECT Id, Name FROM Users;
    
    0 讨论(0)
  • 2021-01-13 03:13

    You can even set a column numerically to keep the order even though UNION ALL is applied

    SELECT 0 RNO, 0 AS Id, 'All' AS Name
    UNION ALL
    SELECT ROW_NUMBER() OVER(ORDER BY  Id)RNO, Id, Name 
    FROM Users
    

    This will work even though a Zero exists in your column Id(may be a junk value)

    0 讨论(0)
提交回复
热议问题