Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Way to alias a database?

 
   Database Help (Home) -> Programming RSS
Next:  delete dupes with a unique id using a temp table  
Author Message
laredotornado

External


Since: Jan 29, 2008
Posts: 6



(Msg. 1) Posted: Wed Jan 05, 2011 11:14 am
Post subject: Way to alias a database?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

I'm using MS Sql 2005. When I run a query from within "MyDatabase"
that accesses another database, "OtherDatabase", is there a way I can
create an alias, "OtherDatabaseOtherName", to access "OtherDatabase"
so that I could run

select * FROM OtherDatabaseOtherName.mytable

and would mean the same thing as

select * FROM OtherDatabase.mytable

? Thanks, - Dave

 >> Stay informed about: Way to alias a database? 
Back to top
Login to vote
Bob Barrows

External


Since: Jan 05, 2011
Posts: 4



(Msg. 2) Posted: Wed Jan 05, 2011 2:42 pm
Post subject: Re: Way to alias a database? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

laredotornado wrote:
> Hi,
>
> I'm using MS Sql 2005. When I run a query from within "MyDatabase"
> that accesses another database, "OtherDatabase", is there a way I can
> create an alias, "OtherDatabaseOtherName", to access "OtherDatabase"
> so that I could run
>
> select * FROM OtherDatabaseOtherName.mytable
>
> and would mean the same thing as
>
> select * FROM OtherDatabase.mytable
>
> ? Thanks, - Dave

So the database used can be changed at runtime? No, you might as well use
dynamic sql. Or use IF, as in:

IF @dbname='db1' select * FROM db1.dbo.mytable
IF @dbname='db2' select * FROM db2.dbo.mytable
etc.
Check out the dynamic sql article at http://www.sommarskog.se/index.html

 >> Stay informed about: Way to alias a database? 
Back to top
Login to vote
Bob Barrows

External


Since: Jan 05, 2011
Posts: 4



(Msg. 3) Posted: Wed Jan 05, 2011 3:24 pm
Post subject: Re: Way to alias a database? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

laredotornado wrote:
> Hi,
>
> I'm using MS Sql 2005. When I run a query from within "MyDatabase"
> that accesses another database, "OtherDatabase", is there a way I can
> create an alias, "OtherDatabaseOtherName", to access "OtherDatabase"
> so that I could run
>
> select * FROM OtherDatabaseOtherName.mytable
>
> and would mean the same thing as
>
> select * FROM OtherDatabase.mytable
>
> ? Thanks, - Dave
Just to add:
if the reason for this is you need to move the code from one environment to
another (perhaps a dev server to a production server) without modifying the
code, you can use synonyms. The result will not be two-part names: you can
create synonyms to point to objects in a database, not the database itself.
So instead of
OtherDatabase..mytable1, OtherDatabase..mytable2, you will have:

OtherDatabasemytable1, OtherDatabasemytable2

The definition of the synonyms can be different. For example, in the dev
MyDatabase, OtherDatabasemytable1 and point at a table in OtherDatabaseDev,
and in the prod MyDatabase, it can point at the table in OtherDatabaseProd.
 >> Stay informed about: Way to alias a database? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
using a VALUE + a STRING as an ALIAS? - Hi, I'd like to translate this from Access SQL to 'true' SQL: SELECT "http:/mydomain/" & [Image] AS image_url I've tried several things but SQL Server doesn't accept it. How can I add this string to the value of that field? Thank you v...

Table alias with UPDATE ? - Is there a way to use a table alias with an UPDATE that has no FROM clause? I'm using SQL 2000. For example: UPDATE TableLongName SET myfield3 = 789 WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456 works, but isn't as easy to read...

Using the field alias name in WHERE clause - Hi, Is there anyway to use the field alias name within the WHERE part of a SQL statement in MS SQL Server 2005? Below is an example: Select Table1.Name as FullName, Table1.Address as Address from Table1 Where FullName like '%test%' What..

using kind of alias in sqlplus - hi all. Can someone please help me on the following. Example query: Tabel1 contains a lot of items. TABLE1 ITEM VARCHAR2(30), FAM1 VARCHAR2(10), FAM2 VARCHAR2(10), FAM3 VARCHAR2(10), FAM4 VARCHAR2(10) Table contains a lot of records with....

Order by [alias] in 2005 issue - So this 'order by' works in SQL2000, but will error in 2005 - select top 1 a.forderID [orderId] from MyTable a order by a.orderid SQL2005 eRR: Msg 207, Level 16, State 1, Line 2 Invalid column name 'orderid'. How to work around? Is there a server...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]