IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact
You are here: Home / How To / How to Get Table Definition in SQL Server

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: SQL, SQL Server

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • .NET
  • Coding
  • Cybersecurity
  • Database
  • How To
  • Internet
  • Multimedia
  • Photography
  • Programming
  • Resources
  • Review
  • WordPress
  • Writing

Recent Posts

  • F#: Seq, List, Array, Map, Set. Which One to Use?
  • How to Get Table Definition in SQL Server
  • Why You Need to Use a VPN
  • How to Use Custom Color in SSMS Using Redgate SQL Prompt
  • How to Install Python on Windows Server
Web Hosting

Recent Posts

  • F#: Seq, List, Array, Map, Set. Which One to Use?
  • How to Get Table Definition in SQL Server
  • Why You Need to Use a VPN
  • How to Use Custom Color in SSMS Using Redgate SQL Prompt
  • How to Install Python on Windows Server
  • RSS

Tags

.NET Core Access Adobe AdSense Amazon ASP.NET Cdonts Dll Classic ASP Code Editor Connect-It Copywriting ETL FSharp Genesis Framework Git Google HP Asset Manager HTML HTML5 Hugo IIS Information Security Internet Internet Information Services iOS Linux macOS Microsoft Microsoft SQL Server MVC PHP Simple Mail Transfer Protocol Smtp Server Social Media SQL SQL Server SSIS SSMS SSRS VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

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