How can I do the equivalent of "SHOW TABLES" in T-SQL? - 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.

Related

SQL Select list of tables in a database

I am using SQL Server 2005. I am trying to SELECT a list of tables in one of my database. Here is my structure of my SQL Server:
- <IP>(SQL Server 9.0 -userlogin)
- Databases
- Company
- Tables
- dbo.UserDB
- dbo.detailsDB
- dbo.goodsDB
I would like to retrieve the values of dbo.UserDB, dbo.detailsDB, dbo.goodsDB
But i do not know what is the exact sql query needed.
I have tried many ways like
SELECT * FROM userlogin.Tables; and
SELECT * FROM userlogin.Company.Tables;, but none of them works.
I have seen quite a few posts which suggests using show databases and show tables, but they don't seem to work as well.
Is it possible to select a list of table names in a database in the first place?
Thank you for any help in advance.
Thanks for the MSDNA link that #TomTom provided, I can now list my tables in my database.
However, I would want to list specific tables where TABLE_NAME contains "user".
How can I do it? I am trying on the following sql but it is not displaying result at the moment:
SELECT DISTINCT TABLE_NAME
FROM Company.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%"user"%';
GO
Try the iNFORMATION_SCHEMA.
http://msdn.microsoft.com/en-us/library/ms186778.aspx
They contain all the schema information you need.
Use the new sys system catalog in SQL Server 2005 and up:
SELECT Name
FROM sys.tables
WHERE is_ms_shipped = 0 -- only list *your* tables - not the system / MS table
You can read more about system catalog views and how to query them in the MSDN Books Online - and be warned - there's lots more to read and learn!
You could use this
Use ***database name***
SELECT *
FROM sys.tables
WHERE name like '%user%'
Sorry, have seen that #marc_s has provided the answer before me!
You can use INFORMATION_SCHEMA as told by #TomTom:
USE <<YOUR_DB_NAME>>
SELECT TABLE_SCHEMA + '.' + TABLE_NAME, *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_SCHEMA + '.' + TABLE_NAME
You can customize above query as per your requirements.
For your other question to find tables where the name contains 'user', execute the following statement:
USE <<YOUR_DB_NAME>>
Exec sp_tables '%user%'
Try:
SELECT *
from sysobjects
where type = 'U' AND
NAME LIKE '%user%'
GO

Oracle: SQL query to find all the triggers belonging to the tables?

how can i find all the triggers that belong to a table?
The following will work independent of your database privileges:
select * from all_triggers
where table_name = 'YOUR_TABLE'
The following alternate options may or may not work depending on your assigned database privileges:
select * from DBA_TRIGGERS
or
select * from USER_TRIGGERS
Check out ALL_TRIGGERS:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2107.htm#i1592586
Another table that is useful is:
SELECT * FROM user_objects WHERE object_type='TRIGGER';
You can also use this to query views, indexes etc etc
Use the Oracle documentation and search for keyword "trigger" in your browser.
This approach should work with other metadata type questions.

Mysql - find a table in all database

Is there any command that I can locate a certain table from all databases? Since I forgot where the table is. Thanks.
Use the MySQL specific:
SHOW TABLES
...or use the ANSI standard, INFORMATION_SCHEMA.TABLES:
SELECT table_name,
table_schema AS dbname
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name='searched table name'

Portable SQL to determine if a table exists or not?

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.

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'