Thursday, March 8, 2012

Database Alias

Hi,
Is there any way to set up an alias for a database within an SQL script?
What I'm trying to acheive is... instead of having -
<DatabaseName>.dbo.table, <DatabaseName2>.dbo.table, etc. - throughout the
script, I just set up an alias at the top of the script and reference them.

TIA
BenPrefix your query with USE...

USE dbname
Select * from tblName

You can also alias table names by putting the alias after the table name in
the from clause...

Select p.firstName, p.lastName from tblPeople p

--
Jerry Boone
Analytical Technologies, Inc.
http://www.antech.biz

"Ben" <ben.kinslow.nospam@.baesystems.plznospam.com> wrote in message
news:3fbb5e8e$1@.baen1673807.greenlnk.net...
> Hi,
> Is there any way to set up an alias for a database within an SQL script?
> What I'm trying to acheive is... instead of having -
> <DatabaseName>.dbo.table, <DatabaseName2>.dbo.table, etc. - throughout the
> script, I just set up an alias at the top of the script and reference
them.
> TIA
> Ben|||Thanks a lot... but...
I have a script that accesses two databases throughout. I *guessed* that
you could only have one USE statement in effect at any one point.

I am new to this, so any help is useful.

Jerry Boone <jerry@.antech.biz.killspam> wrote in message
news:N7Nub.1447$5x1.4@.newssvr22.news.prodigy.com.. .
> Prefix your query with USE...
> USE dbname
> Select * from tblName
>
> You can also alias table names by putting the alias after the table name
in
> the from clause...
> Select p.firstName, p.lastName from tblPeople p
>
> --
> Jerry Boone
> Analytical Technologies, Inc.
> http://www.antech.biz
>
> "Ben" <ben.kinslow.nospam@.baesystems.plznospam.com> wrote in message
> news:3fbb5e8e$1@.baen1673807.greenlnk.net...
> > Hi,
> > Is there any way to set up an alias for a database within an SQL script?
> > What I'm trying to acheive is... instead of having -
> > <DatabaseName>.dbo.table, <DatabaseName2>.dbo.table, etc. - throughout
the
> > script, I just set up an alias at the top of the script and reference
> them.
> > TIA
> > Ben|||Also

declare @.tblname varchar(100)
set @.tblname = '<DatabaseName>.dbo.table'
exec ( 'select * from ' + @.tblname)

On Wed, 19 Nov 2003 12:17:44 -0000, "Ben"
<ben.kinslow.nospam@.baesystems.plznospam.com> wrote:

>Hi,
>Is there any way to set up an alias for a database within an SQL script?
>What I'm trying to acheive is... instead of having -
><DatabaseName>.dbo.table, <DatabaseName2>.dbo.table, etc. - throughout the
>script, I just set up an alias at the top of the script and reference them.
>TIA
>Ben

No comments:

Post a Comment