Myrth (m0sh3_NO@SPAM_hotmail.com) writes:
[quoted text, click to view] > Is there a way to avoid creation of same stored procedures for each
> database as it makes maintenance a hell, but to have for ex. REPORTS
> database with all stored procedures that can access tables in other DBs
> on demand by parameter?
Actually there is: this is the way the system stored procedure works.
However, while you can add your own system stored procedures, this is
deprecated and unsupported, and also has security issues.
And anyway the scheme breaks down the day the databases are spread over
more than one server. In this case, you have no choice than to deploy
your procedure on at least every server.
So the real answer to your problem is to devise a distribution
mechanism, so that you easily can distribute updates to your code to
your databases, no matter they are all on the same server, or spread
out on several customer sites. Obviously for this you need a master
to deliver from, but this master database is not an SQL database -
it's a version control system (which may use SQL Server as its storage
engine, but that's another story).
Such a distribution can be assembled in different ways. The heart of the
procedure is always that in your version-control system you set a
baseline. The terminology changes between version-control systems,
but often you talk about "labels". Then you assemble the changes between
two labels. This can be performed by manually building a BAT-fil which
uses OSQL to install the stored procedures, or it can be more elaborate.
There are several third-party tools on the market. Popular is SQL Compare
from Red Gate,
www.red-gate.com, although it works from a different
perspective than I've outlined here. Myself, I have made the tool we
use in our shop available as freeware, see
http://www.abaris.se/abaperls/. Thus, what you have is a configuration-management problem, not one of
writing stored procedures. It may seem inflexible that stored procedures
are tied to one certain database, but there are some good reasons for
this. The most important is that although the logic in the stored
procedure may be the same in two databases, the query plan that SQL Server
should use to retrieve the data may not, because the data is different
in the two databases.
--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se
Books Online for SQL Server SP3 at