SQL Server : one stored procedure used in many databases - sql-server

I have one main database and I need to replicate it for each client (each client must have one separated database with its own information based on main database).
What is your suggestion to avoid the replication of stored procedures, triggers, functions etc and to have only one code in the main database? But using those procedures, triggers, functions, etc in each database of course.
Thanks and regards.

Related

How to execute a stored procedure present in database A/Server A and query database B/Server B as well?

I have a configurations database with a lot of stored procedures and then I have a large number of databases, all are part of one system and on the same database server.
When the stored procedures in the config database are executing, they often query other databases as well and it is possible to do so because the configuration and all the other database are on the same database server.
But with time, as the data is growing, customers are growing, databases are growing, this one database server is slowing down. So now we want to take some of our databases and put them on a different database server but we are unable to do so because these databases and the configuration database are tightly coupled to each other because many of the stored procedures in the config database query other databases as well.
Is there some way I can execute a stored procedure present in config database / Server A, but this stored procedure is also querying database 2 on Server B?
If not then what could be the best approach to decouple all the other databases from the configurations database? I know getting rid of the stored procedures by implementing an ORM or something could be an option but that would be very time taking as we 1000+ stored procedures.
Let's say your configure database is Server A and all your user databases are on Server A and your SP are using 3 part naming to query multiple databases.
Now you want to migration one of the databases (DB1) to Servers B. Now DB1 does not exist on Server A, it is now on Server B. You can then Create a place holder DB on Server A called DB1. Then you will create a linked server to Server B and in the Place holder DB1, you will create alias which uses the linked server object with the same table name. This way, no changes is required on your thousands of SP and its configuration.
However, Linked Server may introduce performance problems as joining and indexing is less efficient.

Same app for multiple clients and custom stored procedures in SQL Server

We have the same app source (with some custom "if x client then") and basically the same SQL Server database structure. But, some clients need slightly different stored procedures.
What would be best practice in this scenario for long term maintaining the databases and keeping the correct structure? As of now, for example when I change the procedure in one database and need to do the same in 9/10 others I just ALTER procedure and USE different database. But, I can't keep track of which procedures are different in that special snowflake client.
Any ideas? The plan is of course to get more clients so that's looking for trouble.
I try to push the "one fits all" concept but hey, what can you do...
Maybe have that "if x client then" as case statement in a SQL Server stored procedure and then you can just ALTER mindlessly?

System-Level Custom Views in SQL Server

I would like to do the following in SQL Server (2008 + ):
Define a view, let's call it [MySchema].[MyTableInfo], that queries the SQL Server catalog views, e.g., [sys].[tables], to obtain a customized presentation of the underlying catalog metadata.
Install this view somewhere (in master?) so that it can be called from the context of any database on the server and return the metadata appropriate for that context, just as the catalog views do.
I have seen reference to techniques to do something similar with utility stored procedures, but this is a little different. Is what I'm wanting to do possible? If so, how?
Update:
I found an article that described how to do almost exactly what I want but with stored procedures. The routines are stored in the master database and marked as system objects. When they return metadata from the catalog views/information schema, the do so in the context of the current database.
Using stored procedures to execute these queries would be extremely inconvenient for my use case; is there not a way to mark views and/or table-valued functions as system objects and have them execute in the context of the calling database? I have hacked on this without success...

How to separate user-defined messages for different databases

I have 2 databases, each of them has its own user-defined messages. The problem is that their message_id's stored in sys.messages intersect so these DB's cannot be deployed on the same SQL Server instance without changing all messages in one database (but these is too expensive - I have to change ALL stored procedures).
Is there any way to make error messages specific to database?
The sys.message table is database-wide so you simply cannot do that easily. That table really should only be used for things that are DB-wide (like extended stored procedures and other server extensions), not for stored procedure. But I guess changing that is already too late.
I see only two ways around it:
Use the "language" field when adding the message to identify not the message but the application. Of course, that will cause additional problems as you'll need to have each application use it's own "language".
Use two different instances of SQL server, one for each app.

Questions on SQL SERVER System objects

I have a couple of questions.
1) Why cannot we see system tables (like sysobjects) under Master/Model/MSBD etc.? But we
can query. Are we basically querying the views, because as they are the main tables that
holds a value able informations?
Like "SELECT * FROM sysobjects". are we basically querying some views?
2) Why cannot we add triggers to system tables?
Thanks in advance
SQL Server 2008 system tables (http://msdn.microsoft.com/en-us/library/ms179932.aspx) have been implemented as read-only views. One cannot directly work with the data in these system tables. You can access SQL Server metadata using catalog views. Do check this link http://msdn.microsoft.com/en-us/library/ms174365.aspx
It is possible to create triggers on system tables but it is generally not recommended. Please check this http://www.sql-server-performance.com/faq/trigger_system_table_p1.aspx
cheers
Since SQL 2005 the catalog views are implemented as views declared in the Resource Database (mssqlsystemresource). Due to some special magic they appear to exist in every database.
You can always use the execution plan to see from what actual tables do these views fetch data from. The underlying tables can be accessed when you are connected with a DAC connection. Modifying the system tables in any way will mark the database and an message will be logged every time the database starts up. Modified databases are not supported by MS, so if something goes wrong you cannot ask for support.

Resources