Friday, March 23, 2012

How to refer from a stored procedure to a table in another databas

Hi,
I need to select data from a table in my stored procedure. The problem is
that the table I am interested in resides in another database. Hardcoding
fully qualified name is not an option. Database name where the table is -
must be a parameter of SP. Is there any other way than using dynamically
generated query string inside stored procedure? Generated query string is
difficult to maintain.
Any ideas?
Thanks in advance.Alexander Korol wrote:
> Hi,
> I need to select data from a table in my stored procedure. The
> problem is that the table I am interested in resides in another
> database. Hardcoding fully qualified name is not an option. Database
> name where the table is - must be a parameter of SP. Is there any
> other way than using dynamically generated query string inside stored
> procedure? Generated query string is difficult to maintain.
>
Two options that I can think of:
1.A series of IF statements, which I'm pretty sure would be the solution of
choice, given that you don't have more than 10 or so databases ...
2. Another option is to create a view that unions the tables including a
column for source database name (I think this is how partitioning is
implemented )
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

No comments:

Post a Comment