Portable SQL to determine if a table exists or not? - sql

Is there a portable way of determining if a database table already exists or not?

Portable? I don't think so.
Maybe the closest you can get is:
select * from <table>
And this would return an error if the table doesn't exist.

This is as portable as it gets, sadly:
select
count(*)
from
information_schema.tables
where
table_name = 'tablename'
and table_schema = 'dbo'
This definitely works on SQL Server, MySQL, and Postgres. Not so much on Oracle, though. You'd have to access the Oracle data dictionary for that. However, there is an open source project that creates information_schema in Oracle from the data dictionary. You can try that if you need absolute portability.
P.S.-Schema doesn't have to be dbo, but that's the most common.

I would say
select 'x' from <table_name> where 0=1;
The drawback is that if you get an error, you don't know for sure what was the real cause. It might be missing table or e.g. a connection error. You can parse the error message, but definitely it would not be portable.

The INFORMATION_SCHEMA views are ANSI standard - so those should be your most portable option. Don't forget to add the schema and table type to your where clause...
if exists(select *
from information_schema.tables
where table_schema = 'dbo'
and table_name = 'MyTable'
and table_type = 'basetable')
begin
-- your code here
end

Here is something that is reasonably portable:
select now() from TABLE limit 1;
It doesn't rely on knowledge of any particular column.
It doesn't incur the overhead that count(*) sometimes has.
It doesn't matter whether the table is empty or not.
It fails if the table doesn't exist.

As every DBMS has its own metabase, I think the most "portable" way to do this is using the application caller itself. Something like
try
execute("select top 1 * from table")
return (true)
catch
return false

Attempt to query the table. If the query fails -- you get an error, it doesn't exist.
That is probably as portable as you can get. The burden of producing the result then depends on the code querying the table/database.
select top 1 *
from MyTable
Keep the query as simple as possible to prevent other possible errors.

Related

empty sql table only if it exists (not drop)

How can I delete the contents of a table only if it exists? Preferably the sql statement should be standard and not oriented to any db.
Please notice that I do not want to drop a table if it exists, i.e.
DROP TABLE IF EXISTS foo
PS: I have already checked truncate and delete but they don't fit the requirement if the table exists.
As far as i know, there is no standard for "if exists". Some databases support it, others do not, and will give you a syntax exception.
Edit
INFORMATION_SCHEMA does not usually change between different versions and is common in most databases, and to my best knowledge this is the most proper way to check whether a table exists in SQL:
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = tableName ))
BEGIN
DELETE FROM tableName
END

SQL/JDBC : select query on variable tablenames

I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their names start by a particular string.
The query I wrote is the following :
select TABLE_NAME
from MY_TABLE
where TABLE_NAME like '%myString%'
and (select count(*) from TABLE_NAME where rownum=1)<>0
order by TABLE_NAME;`
The problem comes from the second SELECT, but I don't know how can I do to use the TABLE_NAME value.
Does someone have an idea ?
Thanks.
[Added from comments]
Actually, I need to test the V$ views contained in the ALL_CATALOG table. But if I can find another table where all these views are contained too and with a NUM_ROWS column too, it would be perfect !
Standard versions of SQL do not allow you to replace 'structural elements' of the query, such as table name or column name, with variable values or place-holders.
There are a few ways to approach this.
Generate a separate SQL statement for each table name listed in MY_TABLE, and execute each in turn. Brute force, but effective.
Interrogate the system catalog directly.
Investigate whether there are JDBC metadata operations that allow you to find out about the number of rows in a table without being tied to the system catalog of the specific DBMS you are using.
Can you use oracle view USER_TABLES? then query will be much easier
select TABLE_NAME
from USER_TABLES
where TABLE_NAME like '%myString%'
and Num_ROWS > 0
order by TABLE_NAME;`

How does one cheaply validate the existance of a column in a table in another schema with Oracle?

The environment is Oracle 9 & 10. I do not have DBA level access.
The problem is to verify that a specific column exists in a specific table, in another schema.
There are two cases to deal with.
Another schema in the same instance
A schema in a different instance, using a db_link
Given my schema FRED and another schema BARNEY, I tried something like this
SELECT 1
FROM BARNEY.USER_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE'
AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
Which yielded [1]: (Error): ORA-00942: table or view does not exist
After vegging on this awhile, I realized that USER_TAB_COLS, is not really a table. It is a view. I have been selecting from tables all along, but not from a view.
I tried the same thing with my db_link, and was surprised to see data come back. A db_link has an embedded schema_name/password in it, so it seems reasonable to me that it worked, as it effectively logs in to the other schema, which should make the views reachable.
Having Googled around, and worn out my eyeballs on on the mountain of Oracle doc,
I am looking for someone to point me in the correct direction, or at least point out what I am missing.
What techniques are available for getting user table related metadata from a schema in the same instance in order to validate that a specific column exists?
Thanks in advance.
Evil.
+1 for good answers.
Thank you.
You can use the following query:
SELECT 1
FROM ALL_TAB_COLS
WHERE TABLE_NAME = 'SOME_TABLE'
AND COLUMN_NAME = 'SOME_SPECIFIC_COLUMN'
AND OWNER = 'BARNEY';
(User_Tables and User_Tab_Cols are just views on all_tables and all_tab_coumns with a where owner = <Current User> attached to it)
If you're allowed to see the Barney's some_table (i.e. you have been GRANTed at least SELECT privileges on it), then you'll know if the column is there. If you have no rights on the table, you won't be able to get meta information on it.
As with the other replies, normally I use ALL_TAB_COLUMNS for a query like this. But that will only show columns in tables where you have SELECT. And it's select on that column -- in the unlikely event that they've implemented column-level privileges for that table, you may be able to see the table, but not see the specific column of interest. For most of us, that's extremely rare.
DBA_TAB_COLUMNS will show all columns, but you'll need select on it granted to your schema by your DBA. (Actually, you'll need a grant on ALL_TAB_COLUMNS to use it, but that's common in most shops). The DBMS_METADATA PL/SQL Built-in package can also be used, with similar limitations, but I think you'll find it more complicated.
Of course, you can also just try to select a record from barney.some_table.some_column#my_dblink (or whatever pieces of that you're interested in). And then handle the exception. Ugly, I wouldn't recommend it in most situations.
You would use all_tab_columns for that.
But beware that you'll only see what you are allowed to see.
Same instance, different schema:
Select Count(*)
From all_tab_cols
Where owner = 'BARNEY' and
table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';
The count(*) has the advantage of always returning a single row with a value of either 1 or 0, so you do not have to deal with NO_DATA_FOUND errors in PL/SQL.
Across a DB Link, same schema as the one you connect as:
Select Count(*)
From user_tab_cols#MY_DB_LINK
Where table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';
Across a DB Link, different schema than the one you connect as:
Select Count(*)
From all_tab_cols#MY_DB_LINK
Where owner = 'BARNEY' and
table_name = 'SOME_TABLE' and
column_name = 'SOME_SPECIFIC_COLUMN';

What's the preferred way to return an empty table in 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.
Having just run both:
SELECT TOP 0 * FROM Table
and
SELECT * FROM Table WHERE 1=0
They produce exactly the same execution plan.
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.
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'

How can I do the equivalent of "SHOW TABLES" in T-SQL?

I would like to do a lookup of tables in my SQL Server 2005 Express database based on table name. In MySQL I would use SHOW TABLES LIKE "Datasheet%", but in T-SQL this throws an error (it tries to look for a SHOW stored procedure and fails).
Is this possible, and if so, how?
I know you've already accepted an answer, but why not just use the much simpler sp_tables?
sp_tables 'Database_Name'
This will give you a list of the tables in the current database:
Select Table_name as "Table name"
From Information_schema.Tables
Where Table_type = 'BASE TABLE' and Objectproperty
(Object_id(Table_name), 'IsMsShipped') = 0
Some other useful T-SQL bits can be found here: http://www.devx.com/tips/Tip/28529
Try this:
USE your_database
go
Sp_tables
go
Try this
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'Datasheet%'
One who doesn't know the TABLE NAME will not able to get the result as per the above answers.
TRY THIS
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo';
Try this :
select * from information_schema.columns
where table_name = 'yourTableName'
also look for other information_schema views.
And, since INFORMATION_SCHEMA is part of the SQL-92 standard, a good many databases support it - including MySQL.
Try following
SELECT table_name
FROM information_schema.tables
WHERE
table_name LIKE 'Datasheet%'
Try it :
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%'
MS is slowly phasing out methods other than information_schema views. so for forward compatibility always use those.
I know this is an old question but I've just come across it.
Normally I would say access the information_schema.tables view, but on finding out the PDO can not access that database from a different data object I needed to find a different way. Looks like sp_tables 'Database_Name is a better way when using a non privileged user or PDO.