views:

1321

answers:

3

Hey guys, is it possible to extend query results with literals like this?

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

or

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

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

Thanks!

+2  A: 

is it possible to extend query results with literals like this?

Yes.

Select Name
From Customers
UNION ALL
Select 'Jason'
  • Use UNION to add Jason if it isn't already in the result set.
  • Use UNION ALL to add Jason whether or not he's already in the result set.
David B
I'm using sql server 2005 and running a simple union like my first example, but I get "Incorrect Syntax". Is it explicitly like my example?
Sorry, misread your example. see edit.
David B
+6  A: 

You use it like this:

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

Use UNION ALL to allow duplicates: if there is a 25-years old Betty among your users, the second query will not select her again with mere UNION.

Quassnoi
+4  A: 

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
Nebakanezer
He specified sql server: "from dual" is oracle only
Joel Coehoorn
+1 for considering us poor corporate oraclists
Martlark