-- To list all the tables in a database.
SELECT *
FROM information_schema.tables
--List all the views in a database
SELECT *
FROM information_schema.tables
WHERE table_type = '
view'
--List all the tables in 'MeinTabelle' catalog excluding some of the system tables
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_CATALOG = '
MeinTabelle'
AND TABLE_TYPE = '
BASE TABLE'
AND TABLE_NAME != '
dtproperties'
AND TABLE_NAME != '
sysconstraints'
AND TABLE_NAME != '
syssegments'
AND TABLE_NAME != '
sysdiagrams'
ORDER BY TABLE_NAME
ASC
--List all the columns in the database that has the word '%mit%' in it.
SELECT *
FROM information_schema.columns
WHERE column_name
LIKE '
%mit%'
--List all columns in the database that are identity fields.
SELECT INFORMATION_SCHEMA.COLUMNS.*
from INFORMATION_SCHEMA.COLUMNS
WHERE
(
SELECT COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, '
IsIdentity')) = 1
--List all columns in the database that are computed fields.
SELECT INFORMATION_SCHEMA.COLUMNS.*
from information_schema.columns
where
(
SELECT COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, '
IsComputed'))=1
--There are two more important information_schema views that are useful in retrieving the table constraints,
--keys and indexes. They are information_schema.table_constraints and information_schema.key_column_usage
--List all the primary key columns in the database.
SELECT K.*
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE K
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
ON K.CONSTRAINT_NAME = C.CONSTRAINT_NAME
WHERE C.CONSTRAINT_TYPE = '
PRIMARY KEY'
-- get the column list for all tables...
SELECT
X.table_name,
[Col_List] =
(
SELECT
TOP 1
STUFF(( SELECT '
, ' + T2.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
AS T2
WHERE T2.TABLE_NAME = T1.TABLE_NAME
FOR XML PATH ('
')), 1, 2,'
')
AS COL_NAMES
FROM INFORMATION_SCHEMA.COLUMNS
AS T1
WHERE T1.TABLE_NAME = X.table_name
ORDER
BY T1.ORDINAL_POSITION
)
FROM information_schema.tables
as X
WHERE X.table_type = '
base table'
ORDER BY X.table_name