IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact

How to Get Table Definition in SQL Server

Whenever you need to pull a quick definition of a table in SQL Server. Here’s the SQL command (T-SQL) to get the table definition in a database:

USE [MyDB]
GO

SELECT COLUMN_NAME,
  CASE
    WHEN ISNULL(NUMERIC_PRECISION, 200) <> 200 THEN DATA_TYPE + '('
         + CAST(NUMERIC_PRECISION AS VARCHAR(5))
         + ',' + CAST(NUMERIC_SCALE AS VARCHAR(5))
         + ')'
    WHEN ISNULL(CHARACTER_MAXIMUM_LENGTH, 0) = 0 THEN DATA_TYPE
  ELSE
    DATA_TYPE + '('
      + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10))
      + ')'
  END AS DATA_TYPE,
  IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '[MyTable]'
GO

The result would be something similar to this:

COLUMN_NAMEDATA_TYPEIS_NULLABLE
AccountNoint(10,0)YES
AccountNamevarchar(50)YES
Balancemoney(10,2)YES
UpdateDatedatetimeNO

You need to substitute [MyDB] and [MyTable] to the database name and table name that you’re querying. That’s all there is to it.

Further Reading

How to Get Names of All Tables in SQL Server
How to Find a Column Name in SQL Server Database
How to Show Field Values as Columns in SQL Server

January 21, 2020 Filed Under: How To Tagged With: Microsoft SQL Server, SQL, SQL Server

Buy me a coffee?

Buy me a coffee If you find any of the articles or demos helpful, please consider supporting my work here, you'll have my big thanks!

Buy me a coffee Support this site
Buy Me a Coffee?

Categories

  • .NET
  • Coding
  • Cybersecurity
  • Database
  • How To
  • Internet
  • Multimedia
  • Photography
  • Programming
  • Resources
  • Review
  • Tips and Tricks
  • Uncategorized
  • Use Case
  • WordPress
  • Writing

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Tags

.NET .NET Core AdSense ASP.NET Cdonts Dll Classic ASP Code Editor ETL FSharp Genesis Framework Git Google HP Asset Manager HTML5 Hugo IIS Information Security Internet Internet Information Services iOS JAMStack Linux macOS Microsoft Microsoft SQL Server MVC PHP PowerShell Python Simple Mail Transfer Protocol Smtp Server SQL SQL Server SSIS SSMS SSRS Sublime Text Visual Studio Visual Studio Code VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

Copyright © 2011-2025 IT Nota. All rights reserved. Terms of Use | Privacy Policy | Disclosure