• We have code similar to this to manage EAV tables.*

    I believe the dynamic SQL executing DDL in a loop reduces your database to a very expensive scripting environment for running procedural code.

    I would propose moving this operation outside the database to either a reporting engine or a scripting/app tier. I can appreciate that this posted solution "works" - but I questions whether this kind of code should be allowed. I know, it's a single ad-hoc solution... but this technique can quickly become standard procedure. When there are a dozen developers writing ad-hoc solutions into production, you may be paying a high price for such 'convenience.'

    *