Skip to content

Instantly share code, notes, and snippets.

@pilotgeraldb
Created May 11, 2016 15:28
Show Gist options
  • Save pilotgeraldb/862e2c5747e0d7dc558c05ebd62c216b to your computer and use it in GitHub Desktop.
Save pilotgeraldb/862e2c5747e0d7dc558c05ebd62c216b to your computer and use it in GitHub Desktop.
retrieves table information in mssql
DECLARE @tablename varchar(50)
SET @tablename = '' -- <--EDIT THIS VALUE
DECLARE @columnname varchar(50)
SET @columnname = '' -- <--EDIT THIS VALUE
SELECT
SCHEMANAME = SCHEMA_NAME(SYSTABLES.SCHEMA_ID),
TABLENAME = SYSTABLES.NAME,
COLUMNNAME = _SYSCOLUMNS.NAME,
DataType = (SELECT TOP 1 NAME FROM SYS.TYPES _SYSTYPES WHERE _SYSCOLUMNS.system_type_id = _SYSTYPES.system_type_id),
DataLen = _SYSCOLUMNS.max_length
FROM SYS.TABLES SYSTABLES
JOIN SYS.COLUMNS _SYSCOLUMNS ON SYSTABLES.OBJECT_ID = _SYSCOLUMNS.OBJECT_ID
WHERE SYSTABLES.NAME = @tablename AND _SYSCOLUMNS.NAME LIKE @columnname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment