INFORMATION_SCHEMA.TABLES behavior? - sql

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'

Related

Get current table name in query

Is it possible to get table name in select statement?
Example:
SELECT Id, "Users" as TableName
FROM Users
I don't want to write "Users" manually but to determine it based on FROM statement.
I'm doing some migrations from one database to another (with different structure), and I have mapping table that stores old table names so that's why I need this (it's less error prone).
You can use following sub-query to get this information (works on SQL Server 2012+):
SELECT *,
(select object_name(ind.object_id)
from sys.fn_PhysLocCracker(%%physloc%%) plc
INNER JOIN SYS.DM_DB_DATABASE_PAGE_ALLOCATIONS(DB_ID(),null,null,null,null) ind
ON ind.allocated_page_file_id = plc.file_id
AND ind.allocated_page_page_id = plc.page_id) as table_name
FROM [your table]
It is painfully slow, caching on the side page allocation will speed things up.
Alternatively, if you want to do it for all tables, you can use following stored procedure:
USE [DB_NAME]
EXEC sp_msforeachtable 'SELECT Id, ''?'' AS [TableName] FROM ?'
Maybe it will be helpful for somebody :)

Get the schema name for specific table in SQL/Oracle

I am trying to get the schema names for below table on Oracle. Could you please help me to understand whether below query is checking for all the schemas or not.
select * from all_tables
where table_name like '%ELEC_SURROGATE_KEY';
Hoping for some help here.
Thanks!
all_tables will show you all tables granted (directly or via role) to the user executing that query.
So, it is possible that there are tables matching like '%ELEC_SURROGATE_KEY', that does not show up in your query.
In SQL:
select TABLE_SCHEMA
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE'%YourTableName%'

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

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

What is Mysql select statement to show a list of tables?

I want to write a program that can show the user a list of tables in the database and also show the descriptions of those tables. So can I do a "select * from system_table" or something like that?
This will give you a list of tables:
show tables;
To describe each table:
describe table_name;
To get both at the same time try:
SELECT * FROM DOMAIN.TABLES WHERE TYPE = 'TABLE'
SELECT * FROM DOMAIN.COLUMNS WHERE TABLETYPE = 'TABLE'
The results are similar to MySql show and describe statements
In addition to show tables, MySQL 5.0+ also supports the INFORMATION_SCHEMA meta-database:
SELECT table_name, table_comment FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name';
information_schema.tables also has other information in it, if you're curious.
Note that if you didn't provide a comment when you created the table and use InnoDB, it will fill the table_comment column with unnecessary data, such as the InnoDB space reserved for this table or the foreign key constraints.