tags:

views:

443

answers:

4

I know I can return an empty table using the following query :

select * from tbFoo where 1=2

but that code doesn't look nice to me.

Is there a 'standard' way of doing this?

If you're wondering why I want to do such a strange thing, it's because I can't name the datatables I return from a stored procedure, so I need empty placeholders.

+1  A: 

Most of the time I see 1=0 but yes thats pretty much the standard approach when you really have to. Although really having to is rare.

AnthonyWJones
Ok. Are there any performances implication depending on the table I choose for my select? I guess not...
Brann
I doubt there is any big performance issue, the optimizer is going to see that there is no need to read anything from the DB, take a look at the execution plan.
AnthonyWJones
A: 

select * from tbFoo where 1=2 is what I've used in the past. Not sure if its a "standard" way but I've used it many times and seen it used by others.

schooner
+1  A: 

What you really need is information_schema, using it will allow you to find out the definition of a table.

You don't mention which database you are using, so here is a link about information_schema Support in MySQL, PostgreSQL (and MSSQL, Oracle, Etc)

An example from the site;

SELECT table_name, column_name, is_nullable, data_type, character_maximum_length
FROM INFORMATION_SCHEMA.Columns
WHERE table_name = 'employees'

In your case, all you need are the column names;

SELECT column_name 
FROM INFORMATION_SCHEMA.Columns
WHERE table_name = 'employees'
garrow
+3  A: 

Having just run both:

SELECT TOP 0 * FROM Table
and
SELECT * FROM Table WHERE 1=0

They produce exactly the same execution plan.

GateKiller
@GateKiller : this looks better :)
Brann
what about select top 0 1 from Table ?
Brann
+1. The accepted answer should net you more than +15.
George Stocker