Having fun with information schema

time to read 2 min | 352 words

Here is a quick way to tell if you are working on legacy code or not. Just run this code and check the result, I even including some text to help you decide.

 

SELECT TOP(1)

      table name,

      COUNT(table name) as NumOfCols,

      CASE

            WHEN COUNT(table name) < 15 THEN 'No.'

            WHEN COUNT(table name) < 25 THEN 'Not yet.'

            WHEN COUNT(table name) < 35 THEN 'Seriously think about refactoring.'

            WHEN COUNT(table name) < 55 THEN 'Yes!'

            WHEN COUNT(table name) < 85 THEN 'Hell, Yes!'

            ELSE 'Run Away!'

      END as [Is Legacy]

FROM INFORMATION SCHEMA.COLUMNS

GROUP BY table name

ORDER BY NumOfCols DESC

 

As a note, running this query against my current database, it tells me to run far away.