How can I connect to an external database from a sql statement or a stored procedure?

后端 未结 4 1324
说谎
说谎 2020-12-17 21:47

When running a SQL statement or a stored procedure on a database, can you connect to an external database and pull data from there?

something like:

S         


        
4条回答
  •  孤城傲影
    2020-12-17 22:35

    Yep -- there's two methods: either use the function OPENROWSET, or use linked servers. OPENROWSET is useful for ad-hoc single statements, but if you're going to be doing this regularly, read up on linked servers as they allow you to do exactly what you've specified in your SQL Statement ... e.g.,

    SELECT database.owner.table for local data
    SELECT server.database.owner.table for remote data

    And yes, you can mix and match to do joins twixt local and remote. Note though that you'll need to be caureul if you do joins against large tables that exist on the remote server as the query could take a long time to exexute...

提交回复
热议问题