Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

MSSQL – Table/View Column Information

Posted on February 25, 2013October 26, 2015 By David Kittell

Similar to MySQL – Table/View Column Information, this post will help with MSSQL tables and columns.

exec sp_columns '<tablename>'

Cleaner Result:

SET NOCOUNT ON

DECLARE @tablename NVARCHAR(max)

SET @tablename = <tablename>

SELECT c.NAME 'Column Name'
	,t.NAME 'Data type'
	,c.max_length 'Max Length'
	,
	--c.precision ,
	--c.scale ,
	CASE c.is_nullable
        WHEN 0
            THEN 'No'
        WHEN 1
            THEN 'Yes'
        END AS 'Nullable',
    CASE ISNULL(i.is_primary_key, 0)
        WHEN 0
            THEN 'No'
        WHEN 1
            THEN 'Yes'
        END AS 'Primary Key'
FROM sys.columns c
INNER JOIN sys.types t ON c.system_type_id = t.system_type_id
LEFT JOIN sys.index_columns ic ON ic.object_id = c.object_id
	AND ic.column_id = c.column_id
LEFT JOIN sys.indexes i ON ic.object_id = i.object_id
	AND ic.index_id = i.index_id
WHERE c.object_id = OBJECT_ID(@tablename)
	AND t.NAME <> 'sysname'
ORDER BY c.column_id

All Tables Result:

SELECT t.NAME AS 'Table Name',
	c.NAME AS 'Column Name',
	ty.NAME AS 'Data type',
	c.max_length AS 'Max Length',
	CASE c.is_nullable
		WHEN 0
			THEN 'No'
		WHEN 1
			THEN 'Yes'
		END AS 'Nullable',
	CASE ISNULL(i.is_primary_key, 0)
		WHEN 0
			THEN 'No'
		WHEN 1
			THEN 'Yes'
		END AS 'Primary Key'
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN sys.types ty ON c.system_type_id = ty.system_type_id
LEFT JOIN sys.index_columns ic ON ic.object_id = c.object_id
	AND ic.column_id = c.column_id
LEFT JOIN sys.indexes i ON ic.object_id = i.object_id
	AND ic.index_id = i.index_id
WHERE ty.NAME <> 'sysname'
ORDER BY 'Table Name',
	c.column_id
Originally Posted on February 25, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MSSQL - Audit

Post navigation

Previous post
Next post

Related Posts

C# WinForm Write Text Log

Posted on July 31, 2014October 26, 2015

public void LogMessage (string msg) { string sFilePath = Environment.CurrentDirectory + "Log_" + System.AppDomain.CurrentDomain.FriendlyName + ".txt"; System.IO.StreamWriter sw = System.IO.File.AppendText(sFilePath); try { string logLine = System.String.Format( "{0:G}: {1}.", System.DateTime.Now, msg); sw.WriteLine(logLine); } finally { sw.Close(); } } LogMessage("Problem with program"); Originally Posted on July 31, 2014Last Updated on October 26,…

Read More

Classic ASP Send Email

Posted on March 3, 2014May 25, 2017

<% Set myMail=CreateObject("CDO.Message") if Request("subject") <> "" then myMail.Subject=Request("subject") else myMail.Subject="<Subject>" end if if Request("from") <> "" then myMail.From=Request("from") else myMail.From="<From Email Address>" end if if Request("To") <> "" then myMail.To=Request("To") else myMail.To="<To Email Address>" end if if Request("Cc") <> "" then myMail.CC=Request("Cc") else myMail.CC="<CC Email Address>" end if myMail.TextBody=…

Read More

PowerShell – Azure – Create UNIX VM

Posted on April 8, 2016

If you have a need to create a few VMs it can get a bit tedious to build them in one of the various portals. Set the variables then let this script run. At some point I will create a function around this but for now the script works as…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes