Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I make a select which always returns zero rows

Tags:

java

sql

jdbc

I want to determine if a column exists in a table for any jdbc driver.

In order to know the columns in a table, I have to make a query to the table and then get the ResultSetMetaData for the info, but this is pretty expensive in like 99% of times.

In mysql I have:

SELECT * FROM tablename LIMIT 0,0

In Intersystems caché I have:

SELECT TOP 0 * FROM tablename

But, for example, in JavaDB I cannot apply any limit at all.

Is there any generic query that would give me the same result and still be fair with the DB performance?

Thanks in advance.

like image 943
Oso Avatar asked Jun 29 '09 15:06

Oso


5 Answers

SELECT * FROM tableName WHERE 'You' = 'Smart'
like image 159
Slim Avatar answered Oct 06 '22 01:10

Slim


...or you could just use the DatabaseMetaData route.

There's a "getColumns" method that does what you need without creating a resultset.

like image 45
Jeremy Smyth Avatar answered Oct 06 '22 00:10

Jeremy Smyth


SELECT * FROM table_name WHERE 1 = 2;

like image 43
Jeff Ferland Avatar answered Oct 06 '22 00:10

Jeff Ferland


select * from tablename where 1 != 1
like image 40
davetron5000 Avatar answered Oct 06 '22 00:10

davetron5000


Unrelated, but if you just need the columns in MySQL, you can run SHOW FIELDS FROM tablename. This returns columns and the associated info.

like image 36
Ben Hughes Avatar answered Oct 06 '22 00:10

Ben Hughes