Database Alias

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    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
    Ben


  • Jerry Boone

    #2
    Re: Database Alias

    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.



    "Ben" <ben.kinslow.no spam@baesystems .plznospam.com> wrote in message
    news:3fbb5e8e$1 @baen1673807.gr eenlnk.net...[color=blue]
    > 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[/color]
    them.[color=blue]
    >
    > TIA
    > Ben
    >
    >[/color]


    Comment

    • Ben

      #3
      Re: Database Alias

      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.b iz.killspam> wrote in message
      news:N7Nub.1447 $5x1.4@newssvr2 2.news.prodigy. com...[color=blue]
      > Prefix your query with USE...
      >
      > USE dbname
      > Select * from tblName
      >
      >
      >
      > You can also alias table names by putting the alias after the table name[/color]
      in[color=blue]
      > the from clause...
      >
      > Select p.firstName, p.lastName from tblPeople p
      >
      >
      > --
      > Jerry Boone
      > Analytical Technologies, Inc.
      > http://www.antech.biz
      >
      >
      > "Ben" <ben.kinslow.no spam@baesystems .plznospam.com> wrote in message
      > news:3fbb5e8e$1 @baen1673807.gr eenlnk.net...[color=green]
      > > 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[/color][/color]
      the[color=blue][color=green]
      > > script, I just set up an alias at the top of the script and reference[/color]
      > them.[color=green]
      > >
      > > TIA
      > > Ben
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Bruce Loving

        #4
        Re: Database Alias

        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.no spam@baesystems .plznospam.com> wrote:
        [color=blue]
        >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
        >[/color]

        Comment

        Working...