• I have recently encountered a case where I have to process a database with literally thousands of views. In these views the first 29 columns are always the same, and the columns start diverging with column 30. One important part of my code is interested in only the first 29 columns. I use two little bits of dynamic SQL to create and drop a SYNONYM of a fixed name on the different views (Books Online has a specific note that SYNONYMs can be created and dropped in dynamic SQL). Then the rest of the ~100 lines of code in my stored procedure can be inline SQL using the SYNONYM to reference the current view of interest. (It's kind of like interfaces in C#.)

    I did not compare performance differences between using the SYNONYMs and having most of the procedure's code in dynamic SQL; whatever little difference there might be doesn't really matter to this particular application. Color coding in the editor and "CREATE PROCEDURE"-time code checking gives me more reliability to my development process to get more correct results.

    I do wonder, though, about the optimizer's behavior when it encounters a SYNONYM. Most of the time it checks the cache based on exact text matches. When the definition of a SYNONYM changes, and the objects referencing it do not, one would hope the optimizer has some notion that things have changed.

    Sincerely,
    Daniel