Since we upgraded to SQL Server 2005 from SQL Server 2000, we have used following script to find out columns with specific datatypes many times. It is very handy small script.
SQL Server 2005 has new datatype of VARCHAR(MAX), we decided to change all our TEXT datatype columns to VARCHAR(MAX). The reason to do that as TEXT datatype will be deprecated in future version of SQL Server and VARCHAR(MAX) is superior to TEXT datatype in features. We run following script to identify all the columns which are TEXT datatype and developer converts them to VARCHAR(MAX)
Script 1 : Simple script to identify all the columns with datatype TEXT in specific database
SELECT OBJECT_NAME(c.OBJECT_ID) TableName, c.name ColumnName
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
WHERE t.name = 'text' --you can change text to other datatypes
ORDER BY c.OBJECT_ID;
GO
Script 2 : Extended script to identify all the columns datatype and other information in specific database
SELECT
OBJECT_NAME(c.OBJECT_ID) TableName
,c.name AS ColumnName
,SCHEMA_NAME(t.schema_id) AS SchemaName
,t.name AS TypeName
,t.is_user_defined
,t.is_assembly_type
,c.max_length
,c.PRECISION
,c.scale
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
ORDER BY c.OBJECT_ID;
Reference : Pinal Dave (https://blog.sqlauthority.com)
65 Comments. Leave new
How to get column Name and value in select.
Example
ID 1
Name KKKK
Address 123 Main
Why do you want that way? how would you handle multiple rows?
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = ‘ABC’ AND TABLE_SCHEMA = ‘XYZ’
i am getting column name from the above query.
But i want to get the value for each column using the select statement. Please advise.
Then you need to do select from table.
Hi, Can someone please advise on how to list the primary key column data types for all the tables in the entire database ?
ace, thankyou
Hi Pinal, Great work.. I need a boolean value for primary key along with the columns datatype and other information in specific database..meaning with the column information, i would like to know if it is a primary key or not. I want it for all the columns in the database, not just based on a table..