Tuesday 25 January 2011

SQL Server - List only NULL columns in a table - Ver 2

Following my last post on how to list all NULL columns within a table in SQL Server, below you can find a second version of the script where you can list the NULL columns of a table in an easier-to-copy format.

Here it goes:


DECLARE @NAME sysname
DECLARE CRS CURSOR

LOCAL FAST_FORWARD FOR
SELECT name FROM syscolumns
WHERE id=OBJECT_ID('<TABLE_NAME>')

OPEN CRS
FETCH NEXT FROM CRS INTO @NAME

WHILE @@FETCH_STATUS = 0
BEGIN

      EXEC('IF EXISTS( SELECT ''' + @NAME + '''
            WHERE NOT EXISTS
                  (SELECT * FROM <TABLE_NAME>
                  WHERE ' + @NAME + ' IS NOT NULL))
            BEGIN
                  PRINT ''' + @NAME + '''
            END'
      )

      FETCH NEXT FROM CRS INTO @NAME

END

CLOSE CRS
DEALLOCATE CRS


Again, I hope you found this useful.

See ya laterz....

No comments:

Post a Comment