Add row to query result with select

Is it possible to extend query results using such literals?

select name from users union select name from ('JASON'); 

or

 select age, name from users union select age, name from (25,'Betty'); 

therefore it returns all the names in the table plus 'JASON', or (25, 'Betty').

+48
sql sql-server tsql select insert
Apr 10 '09 at 19:29
source share
3 answers

You use it as follows:

 SELECT age, name FROM users UNION SELECT 25 AS age, 'Betty' AS name 

Use UNION ALL to allow duplicates: if you have a 25 year old Betty, the second query will not select her again with a simple UNION .

+62
Apr 10 '09 at 19:36
source share

In SQL Server, you would say:

 Select name from users UNION [ALL] SELECT 'JASON' 

In Oracle, you would say

 Select name from user UNION [ALL] Select 'JASON' from DUAL 
+17
Apr 10 '09 at 19:37
source share

Is it possible to extend query results using such literals?

Yes.

 Select Name From Customers UNION ALL Select 'Jason' 
  • Use UNION to add Jason if it is not already in the result set.
  • Use UNION ALL to add Jason if he is already in the result set.
+9
Apr 10 '09 at 19:33
source share



All Articles