all groups > sql server programming > may 2005 >
You're in the

sql server programming

group:

Best way to select a Constant String from a Table ?


Best way to select a Constant String from a Table ? S Anand
5/16/2005 10:46:02 PM
sql server programming:
Hi,

What is the best way to generate a constant String from a Table (Only 1 row).

I have some sql statements returning a few rows. I want to also return a row
with a Constant String along with this sql statement..

e.g.
Select * from orders
Union
Select 'End of Order Select' from ????

I can try selecting it from orders table, but this should return only one row.

Any ideas ?

--
RE: Best way to select a Constant String from a Table ? S Anand
5/16/2005 10:59:01 PM
Sorry for the trouble, found out the answer that I can just do
Select 'End of Order Select'

[quoted text, click to view]
RE: Best way to select a Constant String from a Table ? Chandra
5/16/2005 11:56:01 PM

hi Anand
This will work fine if you are note selecting any value from a table. If u
require a value
from a table along with a constant value, then u require to do like this:

SELECT TOP 1 'Const Value', <COLUMNS> FROM <TABLE>


you query can be modified as
Select * from orders
Union ALL
Select 'End of Order Select'

try using UNION ALL if u definately want the text to be displayed.
in case of UNION the second table will not display a value if the same value
exists
in the main table.

Hope this gives u a better picture

--
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---------------------------------------



[quoted text, click to view]
Re: Best way to select a Constant String from a Table ? Tibor Karaszi
5/17/2005 12:00:00 AM
A few comments:

[quoted text, click to view]

Use square brackets of double-quotes around the produced column name instead. The column you produce
in the result is an identifier and non-standard identifiers are delimited with double-quotes in SNAI
SQL (and SQL Server) and SQL Server also allow double-quotes. Why SQL Server allow single quotes for
identifiers in this particular case is beyond my understanding, very strange...


[quoted text, click to view]

Don't do SELECT *. It might just have been an example, but imagine of the table structure changes
and you add or remove columns. The UNION won't work.


Also, don't expect the SELECT with a constant to come last unless you do an ORDER BY for the UNION.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]

AddThis Social Bookmark Button