How do I output the schema of my database? I want it to output the design of the database.
Something like this might work:
SELECT TABLE_TYPE, TABLE_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
ORDER BY TABLE_TYPE, TABLE_NAME, COLUMN_NAME
But I can't get it to run correctly. A excel file with table names, their columns, types, primary keys, etc. is what I want.
SELECT * FROM INFORMATION_SCHEMA.TABLES;
... sounds like what you are looking for, sans any formatting you want to do.
select
t.type_desc,
t.name as [table],
c.name as [column],
y.name,
c.max_length
from sys.tables t inner join
sys.columns c on c.object_id = t.object_id inner join
sys.types y on c.system_type_id = y.system_type_id
where y.name <> 'sysname'
order by
t.type_desc,
t.name,
c.name
Below posts would be useful for generating data dictionary
Database Documentation - http://deepakrangarajan.blogspot.com/2011/03/database-documentation.html
Generating a Database Data Dictionary - http://sqlserverdiaries.com/blog/index.php/2011/02/generating-a-database-data-dictionary/
Related
I want to find the tables from all the databases that references a column in a table in one of the database. Can anyone help me here?
Not sure how to proceed
this should do, please see that i have used like operator (where col.name like '%COLUMN_NAME_HERE%'), so it will find matching patterns, if you want to find exact match then please use (where col.name = 'COLUMN_NAME_HERE')
select
schema_name(tab.schema_id) as schema_name
,tab.name as table_name
, col.column_id,col.name as column_name
, t.name as data_type, col.max_length, col.precision
from sys.tables as tab
inner join sys.columns as col on tab.object_id = col.object_id
left join sys.types as t on col.user_type_id = t.user_type_id
where col.name like '%COLUMN_NAME_HERE%'
order by schema_name,table_name, column_id*
How can I limit the following query to return for only tables in the database and exclude views?
USE [Database Name] SELECT * FROM INFORMATION_SCHEMA.COLUMNS
Microsoft discourages the use of INFORMATION_SCHEMA. The way to do it if you don't want to use the sys views:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS AS C
INNER JOIN INFORMATION_SCHEMA.TABLES AS T
ON T.TABLE_SCHEMA = C.TABLE_SCHEMA
AND T.TABLE_NAME = C.TABLE_NAME
WHERE T.TABLE_TYPE = 'BASE TABLE'
Join it to sys.Tables:
SELECT c.*
FROM INFORMATION_SCHEMA.COLUMNS c inner join sys.tables t
on c.Table_Name = t.name
How to select database tables where they have two or more specific columns in them?
The one here displays tables with CustomerName in them. To add another column to look for, it seems I might have to add another subquery within the subquery.
I am looking for a simple query where I can add x number of columns without adding complexity.
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE t.name in (select t.name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID where c.name LIKE '%CustomerID%')
ORDER BY schema_name, table_name;
UPDATE
Would want to use the LIKE operator
Assuming you want tables that contain ALL the column names you provide, an easy way without subqueries could be:
SELECT t.name AS table_name,
SCHEMA_NAME(t.schema_id) AS schema_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.name IN('COL1', 'COL2') -- Column names in here
GROUP BY t.name, SCHEMA_NAME(t.schema_id)
HAVING COUNT(DISTINCT c.name) = 2 -- Match the count of columns in the IN criteria
ORDER BY SCHEMA_NAME(t.schema_id), t.name;
I need to bring a technical report describing all the table with its columns that belongs to a certain database. The thing is, it was required to bring the - let´s call it - descripcion of the tables in this database respecting the order of the columns inside the table.
For example, if I have the table_1 that has the columns Column_D,Column_A,Column_M,Column_E in that particular order, I should bring the report just like this
Table - column
Table_1 Column_D
Table_1 Column_A
Table_1 Column_M
Table_1 Column_E
And so on with All the table in the database.
I am using this query
select t.name, c.name
from sys.columns c inner join sys.tables t
on c.object_id = t.object_id
The problem is that I am getting the information that I want, only that organized by the column field.
Table - column
Table_1 Column_A
Table_1 Column_D
Table_1 Column_E
Table_1 Column_M
But if I add a where clause to the query
select t.name, c.name
from sys.columns c inner join sys.tables t
on c.object_id = t.object_id
where c.object_id = 123
I will have the result that I want but only for that particular table.
Is there a way to do what I need?
Without an order by clause, you are leaving the ordering up to SQL to determine based on optimised query execution. You probably want:
select t.name, c.name
from sys.columns c
inner join sys.tables t
on c.object_id = t.object_id
order by t.name, c.column_id
Replace the DBName with your database name in the below query
select Table_Name, Column_Name
from DbName.information_schema.columns c
order by table_name, ordinal_position
Is it possible to retrieve the list of the name of the columns of a table in a SQL Server database?
This should work with any version of SQL Server.
SELECT c.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_SCHEMA = 'schemaName'
AND c.TABLE_NAME = 'tableName'
ORDER BY c.ORDINAL_POSITION
You can press alt+f1 while the table name is selected, this does the same thing as:
sp_help #objname= 'table_name'
Try this:
SELECT
c.name 'Column Name',
FROM
sys.columns c
INNER JOIN
sys.types t ON c.system_type_id = t.system_type_id
LEFT OUTER JOIN
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
c.object_id = OBJECT_ID('YourTableName')
OR THIS:
SELECT SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
WHERE t.name = 'YourTableName'
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY schema_name, table_name;
Beginning with SQL Server 2005 (and up through SQL Server 2012, as of the writing of this answer), the sp_columns stored procedure can provide you with the list of columns (plus a bunch of other stuff too!) for a specific table. The returned result set will include the COLUMN_NAME column, which has what you are looking for.
To use it, run the following statement in your database:
EXECUTE sp_columns #table_name='YourTableName'
Microsoft's online help for sp_columns can be found here.