I need to write some distributed SQL. I'm accessing databases outside my local instance through Linked Servers.
My problem is: this SQL will run at many customer sites and the database names will be different at each.
So to refer to a table in another instance I of course have to use <LinkedServerName>.<dbname>.<owner>.<object>.
Problem: the <dbname> will be different depending on the customer site. So hardwiring the <dbname> into my scripts is a pretty major problem.
I want to avoid dynamically generating the SQL and executing through sp_executesql.
Is it possible to define a Linked Server in such a way that I can skip over the database name (refered to as "catalog" in the definition). I have a one to one mapping between databases and Linked Servers, so per Linked Server I can assume access to one and only one database.
I'm using SS2000 so synonyms are out of the picture.
In a nutshell: I need to write sciprts that can easily adapt to different database names in all the distributed queries I'm writing. Is it possible to do this in SS2000?
Please help!
Best Regards,
Mike
Use the OPENROWSET function (see BOL topic OPENROWSET (Transact-SQL)).
This will allow you to specify a connection provider (oledb maybe) and connection string, then a query to run on the target server/db.
EXAMPLE:
SELECT a.*
FROM OPENROWSET(<provider name>, <connection string>,
<query>) AS a;
So you could run the same query on different server and different DBs just by adjusting the connection string.
No comments:
Post a Comment