Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server query to get data type for all columns in table?

Tags:

sql-server

I have this SQL Server 2012 query that I am running in SSMS:

SELECT name, max_length, precision, scale, is_nullable 
FROM sys.columns
WHERE object_id = OBJECT_ID('dbo.testtable')

It is returning what I want:

enter image description here

However, I would like to add the data type of the column in this query, so that it looks like what I see in the object explorer. Any thoughts on how to add the data type?

enter image description here

like image 220
a1234 Avatar asked Dec 21 '25 23:12

a1234


2 Answers

Here are a couple of options:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'YourTable'

or

exec sp_help 'YourTable'
like image 78
JimSvoboda Avatar answered Dec 24 '25 01:12

JimSvoboda


SELECT c.name,
       c.max_length,
       c.precision,
       c.scale,
       c.is_nullable,
       t.name
  FROM sys.columns c
  JOIN sys.types   t
    ON c.user_type_id = t.user_type_id
 WHERE c.object_id    = Object_id('dbo.testtable')
like image 43
vzwick Avatar answered Dec 24 '25 01:12

vzwick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!