Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StorProc: Adding a record to a query 3

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
US
I have a sp that is simply doing a simple select. Is there a way to add a record that is not in the table. For example, a simple query, say, returns the following records:

1
2
3
4
5

How can I get:

0
1
2
3
4
5

returned when 0 is not in the table?

[conehead]
 
Code:
select 0 union select numbers from yourTable

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
So if I was adding a string would I just do somthing like:

Code:
select "Here I am!" union select field from yourTable

and what if I was returning more than 1 field?

[conehead]
 
select value1, value2
UNION ALL
Select field1, field from yourtable

-DNG
 
I don't see that it would be a problem, give it a try.

The one important thing to remember with unions is to make sure that all select statements are returning the same datatypes in the same order. For example, you can't union a string and a datetime:
Code:
select 'blah' union select getdate()

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
So if I was adding a string would I just do somthing like:

select "Here I am!" union select field from yourTable

No because your query returns integers

you will have to do

Code:
select "Here I am!"  as field 
union all
select convert(varchar(30),field )
from yourTable

and what if I was returning more than 1 field?
then add spaces or nulls to your top query

Code:
select "Here I am!"  as field,null as field2,'' as field3 
union all
select convert(varchar(30),field ),field2,field3
from yourTable


you have to alias your values or your recordset won't have column names


Denis The SQL Menace
SQL blog:
Personal Blog:
 
Additionally, if you use DNG's query above, remember that union [red]all[/red] will return duplicates. So from your first example, if 0 existed in the database then you would have 2 0s in your result set.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top