Skip to content

Instantly share code, notes, and snippets.

@timgaunt
Created November 2, 2012 10:04
Show Gist options
  • Save timgaunt/3999899 to your computer and use it in GitHub Desktop.
Save timgaunt/3999899 to your computer and use it in GitHub Desktop.
List the length of every field in the table
DECLARE @table SYSNAME, @field SYSNAME
set @table = '## Your table name ##'
CREATE TABLE #Temp (TableName SYSNAME, ColumnName SYSNAME, MaxLength INT)
DECLARE a_cursor CURSOR STATIC
FOR
SELECT name FROM syscolumns c WHERE id = object_id(@table)
OPEN a_cursor
FETCH NEXT FROM a_cursor INTO @field
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #temp
EXEC ('select ''' + @table + ''', ''' + @field + ''', max(len(' + @field + ')) from ' + @table )
FETCH NEXT FROM a_cursor INTO @field
END
CLOSE a_cursor
DEALLOCATE a_cursor
SELECT * FROM #Temp
DROP TABLE #Temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment