Search code examples
sqlt-sqlsql-server-2005linked-server

How to run T-SQL in another DB server?


I want to select some data form another database in another server.

What I know is add a Linked Server, and run the T-SQL.

Is this way is a good choice, or is there any other way to run SQL in other database servers?

thanks.


Solution

  • Adding a linked server is fine.

    You will then probably use the 4 part naming convention

    select * from [server].[database].[Owner or Schema].[tablename]
    

    Or you could look at OPENROWSET (Transact-SQL)

    Includes all connection information that is required to access remote data from an OLE DB data source. This method is an alternative to accessing tables in a linked server and is a one-time, ad hoc method of connecting and accessing remote data by using OLE DB. For more frequent references to OLE DB data sources, use linked servers instead.