SQL Select list of tables in a database - sql

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

Related

How to find a specific table from my database with an sql script

i'm a total beginner with sql / data base but i'm trying to learn on a fake data base.
My problem is that i want to find a specific Table from a Schemas and i only know that in the table name there's "CRH".
I've been searching on every tutorial but none of them seems to be working i'm probably doing something wrong like i don't have the right dependencies.I'm using data beaver and i didn't modified it after the download.
here's my code :
SELECT * FROM SCHEMAS_NAME WHERE name LIKE '%CRH%'
You are using the Oracle 12c DBMS so try this:
SELECT table_name FROM all_tables
WHERE table_name LIKE '%CRH%';
Assuming your database is MS SQL Server: Try following code.
select * from sys.all_objects where type = 'U' and name like '%CRH%'

How can I find a table in SQL Server 2016

How can I find a table in SQL Server 2016?
I could use this to find the table in the current database:
SELECT *
FROM sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
The problem is, I have several databases on the server.
Please try with the below sql query.
select * from [YourDatabaseName].INFORMATION_SCHEMA.TABLES where TABLE_NAME LIKE '%App_Current_Seq_Num%'
sp_tables , sys.tables , information_schema.tables will only give you the list of tables from current selected database. Use the below undocumented stored procedure to run this code for each database, with each resultset showing the list of tables belonging to it.
sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%App_Current_Seq_Num%'''
Another option is to use dynamic SQL .
You can either do
use your_database_name
SELECT *
FROM sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
or
SELECT *
FROM your_database_name.sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
or if you want to search multiple databases at once use a union all clause.
Something like this:
SELECT * FROM database1.sys.Tables
UNION ALL
SELECT * FROM database2.sys.Tables
I used this and it worked perfectly:
USE Master
GO
EXEC sp_MSforeachdb
#command1='use ?; SELECT *
FROM sys.tables
WHERE name LIKE ''%App_Current_Seq_Num%'''
you can use SQL Search
it is a simple search control that you can use in sql server
to find tables/views/stored procedures and etc...
use fast free download from
https://www.red-gate.com/products/sql-development/sql-search/index

INFORMATION_SCHEMA.TABLES behavior?

I have this query below. This query finds on database if that particular table is exists in the database. My question is, by using INFORMATION_SCHEMA.TABLES, is it going to find that table name to the other database? Or in just particular database where you are connected???
select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = N'WebServiceCredentials'
As per the MSDN:
Returns one row for each table in the current database for which the
current user has permissions
You can view all the tables for all databases, but not using INFORMATION_SCHEMA.TABLES
You can using system tables.
You can run this query:
sp_msforeachdb 'select "?" AS dbname, * from [?].sys.tables'

Sql Server 2005 Database table is not visible whereas stored procedure is visible

Sir,
I've a problem regarding Database.
My problem is I've Database script file.When I integtrated the script file in Database.I found all the stored procedure is visible.But tables are not visible.
I wrote the Command as follows:
select * from sysobjects where type='p'
Now all the tables is showing but fields are not.
I wrote the Command to get the fields for particular table_name targeted as
select * from table_name .
But a message is Coming like Invalid Object name as table_name.
Please help me out to find the fields of the table ...
You can use the following query to retrieve the columns of the sought-for table:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your table name'
You can list all columns with:
select * from sys.columns

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.