Friday, March 9, 2012

Reference another database without hardcoding the name of it...

Hi,
I s there any way to access another database without hardcoding the name in
the stored procedure?
for example:
database1.dbo.sp_Test()
begin
select * from database2.dbo.supertable
end
So instead of writing "database2" (since this name can change in test
environments) is there another way to reference it? One way could be to
dynamicly create some sql, where the name is found in a table - are there
others?
So the database name must set at runtime rather than compile time...
CheersIn SQL Server 2005, you could create a synonym.
In SQL Server 2000, the method I use is what you describe, get the name of
the server and/or database and build a dynamic string. <yuck>
"Troy" <Troy@.discussions.microsoft.com> wrote in message
news:658A1290-95D2-40B9-A44B-4779A434F4D1@.microsoft.com...
> Hi,
> I s there any way to access another database without hardcoding the name
> in
> the stored procedure?
> for example:
> database1.dbo.sp_Test()
> begin
> select * from database2.dbo.supertable
> end
> So instead of writing "database2" (since this name can change in test
> environments) is there another way to reference it? One way could be to
> dynamicly create some sql, where the name is found in a table - are there
> others?
> So the database name must set at runtime rather than compile time...
> Cheers|||In SQL Server 2000, you can't dynamically change the database or table
referenced in a select, update, delete statement without resorting to
building the statement in a varchar and then executing it using Exec. Is it
really necessary to prefix the table name with the database? It's best for
the development / test database to be deployed on a seperate server or
instance.
"Troy" <Troy@.discussions.microsoft.com> wrote in message
news:658A1290-95D2-40B9-A44B-4779A434F4D1@.microsoft.com...
> Hi,
> I s there any way to access another database without hardcoding the name
> in
> the stored procedure?
> for example:
> database1.dbo.sp_Test()
> begin
> select * from database2.dbo.supertable
> end
> So instead of writing "database2" (since this name can change in test
> environments) is there another way to reference it? One way could be to
> dynamicly create some sql, where the name is found in a table - are there
> others?
> So the database name must set at runtime rather than compile time...
> Cheers

No comments:

Post a Comment