**Hello All**, I am having 2 environments belonging to 2 similar products. Since the products are similar, the code being maintained is the same. In one environment I have 2 databases and in the other I have either 3 or 4 DBs (based on some business settings). **Illustration**: **Env1 DB** : CommonDB, AccountingDB **Env2 DB** : CommonDB, AccountingDB And MirrorDB The common code checks for the existence of the database in the sysdatabases DB and if that condition passes will the manipulations be done on the (non-mandatory) Database. The code seems to be working until the SQL Server version 2008-R2. But in case of SQL Server 2012, though the code does not get into the 'if' condition, it throws an error message saying - "Invalid Database ". **So my question is** - is there some setting which would make it behave the same way as the version 2008, that it would not throw a compilation error? Would really appreciate if anybody could guide me the right way. Thanks Abhilash.KS
I hesitate to recommend this for several reasons, but if you use dynamic SQL the server is incapable of compiling or attempting to validate that part of the code until it reaches the exec or sp_executesql to actually execute it. If I understand what you want correctly, switching those portions of the code to dynamic sql will avoid your error.