Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the preferred way to return an empty table in SQL?

Tags:

sql

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.

like image 257
Brann Avatar asked Feb 26 '09 10:02

Brann


People also ask

What is empty table in SQL?

The TRUNCATE TABLE statement in SQL is a Data Definition Language (DDL) operation that labels a table's extents for DE allocation (empty for reuse). This procedure removes all data from a table easily, usually bypassing a variety of integrity checking mechanisms.

How do you return a blank value in SQL?

There are two ways to replace NULL with blank values in SQL Server, function ISNULL(), and COALESCE(). Both functions replace the value you provide when the argument is NULL like ISNULL(column, '') will return empty String if the column value is NULL.


1 Answers

Having just run both:

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

They produce exactly the same execution plan.

like image 194
GateKiller Avatar answered Sep 24 '22 15:09

GateKiller