16

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

4

2 Answers 2

32

Here are a couple of options:

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

or

exec sp_help 'YourTable'
1
  • 1
    I can confirm the first command works using PgAdmin 4.13 and Microsoft SQL Server Management Studio 18.3.1. The second command (exec sp_help) only runs for me on MSSMS but not PgAdmin.
    – Benjamin
    Sep 7, 2021 at 14:25
10
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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.