Getting THE DBName and the Server name From the Table

  • Hi,

    For any SQL query ,Can i get the name of the database and the server name from the table and put that in The FROM clause

    something like..

    Select * as DBName from Table1

    set @DBname =DBName

    Select * from @DBName

     

    Or can u suggest the way of getting the DBName and the Server name dynamically and then running the SQLQuery

    Regards

    Jyoti

  • select db_name() - this gets you the name of the current database context

    select @@servername - this gets you the name of the server

  • Here's a working example that executes a 'select *' on dbo.Table:

    DECLARE @SQLString nvarchar(100)

    set @SQLString = N'select * from '+ @@servername + '.' + db_name() + '.dbo.Table'

    PRINT @SQLSTRING

    EXEC(@SQLString)


    Have a good day,

    Norene Malaney

  • Hi,

    Can these commands be used in VB as well?  Or would you recommend a different method?

    Thanks

    - Beach

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply