0

How can i drop all columns from a table where all its values are null ?

i have a huge table (100k+ rows) with around 20+ columns, lots of these columns are not used at all, so i want to remove all columns that is NULL in all rows

Zalaboza
  • 8,899
  • 16
  • 77
  • 142

1 Answers1

1

You could loop through each field in a cursor and perform the check and ALTER in one fell swoop, or you can just run a check in one step to see which fields aren't used:

SELECT MAX(col1) col1
      ,MAX(col2) col2
      ,MAX(col3) col3
      .....
FROM YourTable

Then remove any fields that have a MAX() of NULL:

ALTER TABLE YourTable DROP col2
Hart CO
  • 34,064
  • 6
  • 48
  • 63