SQL Query to display the columns and data types in all the tables

August 6, 2012

SQL Query to display the columns and data types in all the tables

The below mentioned script will display the details in the order of TableName, ColPosition, ColumnName, DataType and Length:

SELECT
IT.TABLE_NAME AS ‘TableName’,
ORDINAL_POSITION AS ‘ColPosition’,
COLUMN_NAME AS ‘ColumnName’,
DATA_TYPE AS ‘DataType’,
CHARACTER_MAXIMUM_LENGTH AS ‘Length’
from INFORMATION_SCHEMA.COLUMNS as IC
inner join INFORMATION_SCHEMA.TABLES as IT onĀ  IT.TABLE_NAME=IC.TABLE_NAME and IT.TABLE_TYPE=’BASE TABLE’
ORDER BY
TableName,
ColPosition

Advertisements

SQL Query to find the column name in all the tables in a database

June 28, 2012

The below query will display the list of all the tables with same column name:

USE <DatabaseBase>
GO
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 AS C ON T.OBJECT_ID = C.OBJECT_ID
WHERE C.name LIKE ‘%<ColumnName>%’
ORDER BY schema_name, Table_Name;


%d bloggers like this: