I have three websites which uses an abstract database structure with tables like: Items, Places, Categories, etc… and stored procedures like GetItemsByCategory, GetRelatedItems, etc… Actually im using exactly the same database structure for these 3 different websites.
From a code perspective im using the same code for all websites (except the HTML which is specific foreach one), and all the common code is in few projects used by all websites, so everytime that i detect a bug (which is in all websites) i just fix it on one place (the common part used by all) and automatically all websites get the fix.
Actually im using Asp.net MVC3 and Sql server.
Everytime i want to extend some funcionality, and i need a new table, stored procedure or something related with database, i have to do the modification in each database.
-
Do you know any approach that i could use to be able to have the same flexibility and do database modifications only one time for all websites?
-
Do you think I’m using a good approach or i should use something different in your opinion?
If the databases are on a single server, you could generate the script for the procedure from Management Studio, and make sure to use the option to "check for object existence" (Tools > Options > SQL Server Object Explorer > Scripting). This will yield something like this (most importantly it produces your stored procedure code as something you can execute using dynamic SQL):
Now that you have this script, you can modify it to work across multiple databases – you just need to swipe the @statement = portion and re-use it. First you need to stuff the databases where you want this to work into a @table variable (or you can put this in a permanent table, if you want). Then you can build a command to execute in each database, e.g.
Alternatively, you could create a custom version of my
sp_msforeachdborsp_ineachdbreplacements:I used to use a tool called SQLFarms Combine for this, but the tool doesn’t seem to exist anymore, or perhaps it has been swallowed up / re-branded by another company. Red Gate has since produced SQL Multi Script that has similar functionality.