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

Ektron Rename Templates

Posted on October 23, 2013October 26, 2015

This script will rename template paths SET XACT_ABORT ON DECLARE @currval NVARCHAR(500) DECLARE @newval NVARCHAR(500) DECLARE @search NVARCHAR(500) DECLARE @replace NVARCHAR(500) SET @search = ‘/Index.aspx’ –string to find SET @replace = ‘/Default.aspx’ –replacement string DECLARE @pos INT DECLARE @id BIGINT BEGIN TRAN DECLARE curs CURSOR LOCAL FAST_FORWARD FOR SELECT template_id…

Read More

Show Stored Procedures

Posted on July 30, 2013October 26, 2015

SELECT so.NAME ,created ,last_altered ,specific_catalog ,routine_definition ,sql_data_access FROM sys.objects so LEFT OUTER JOIN information_schema.routines sr ON so.NAME = sr.specific_name WHERE objectproperty(object_id, N’IsMSShipped’) = 0 AND objectproperty(object_id, N’IsProcedure’) = 1 ORDER BY created DESC ,NAME SELECT * FROM information_schema.routines WHERE routine_type = ‘PROCEDURE’ ORDER BY specific_name SELECT * FROM sys.objects WHERE…

Read More

Alter Table

Posted on June 25, 2013October 26, 2015

With this statement you would need to make sure that the column you wish to change does not currently have NULL values before running it otherwise you will likely have errors. ALTER TABLE <TableName> ALTER COLUMN <ColumnName> int NOT NULL; GO With this statement you need to make sure that…

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