In some situations a SQL Server DBA is required to fetch data from another data source. Other data source may be another instance of SQL Server or any other RDBMS. Also it may be a file. SQL Server 2005 provides two major ways to access data from other data sources. These are
- Linked Servers (may use distributed queries)
- Ad hoc queries with openrowset
In following elaboration of using these two ways I will discuss SQL Server as remote data source.
CONFIGURATION AND USAGE OF LINKED SERVERS
Linked servers provide SQL Server with access to remote data sources that may be SQL Server also or any other relational DBMS or text file. Remote data sources are connected through OLE DB provider. OLE DB selection determines that what kind of distributed query operations may be implemented.
Create Linked Server
To create the linked server, use the system stored procedure sp_addlinkedserver. To create a linked server for a SQL Server instance named test on host DBA, we will have following format
EXEC sp_addlinkedserver @server= 'dbatest', @srvproduct= 'SQL Server' GO
Or if it is default instance then you may just enter host name instead of hostinstance format.
Change Any property of a linked server
After a linked server has been created then you may change its several properties like collation compatibility, collation name, connection timeout, data access and query time out etc. For example to change query time out property to 60 seconds for the created linked server, I will use following system stored procedure
EXEC sp_serveroption @server = 'dbatest' , @optname = 'query timeout', @optvalue = 60 GO
View meta data of a linked server
Now we have a linked server with query time out property changed to 60 seconds. In order to view the meta data of this linked servers use following script.
SELECT [name], data_source,query_timeout, modify_date FROM sys.servers WHERE is_linked = 1 GO
Configure Log In of a Linked Server
Now we have a linked server attached. But to access the data on it we have to map proper logins as required. In following script I have mapped the user dba (my local user) to user sa of linked server (remote data source). If I set the values of parameter @locallogin = null then all my local users will be mapped against the user sa of remote data source. If yoy set the parameter @useself = true then windows authentication will be used for connection to linked server. Also we have to provide password for remote user that is sa in this case.
EXEC sp_addlinkedsrvlogin @rmtsrvname = 'dbatest', @useself = false , @locallogin = 'dba', -- if null Applies to all local logins @rmtuser = 'sa', @rmtpassword = 'test' GO
View Login Information Of Linked Server
To view the login mapping for remote server use the following script.
SELECT s.name LinkedServerName, ll.remote_name, p.name LocalLoginName FROM sys.linked_logins ll INNER JOIN sys.servers s ON s.server_id = ll.server_id LEFT OUTER JOIN sys.server_principals p ON p.principal_id = ll.local_principal_id WHERE s.is_linked = 1 GO
Executing the Queries
Now linked server is configured and logins are also mapped properly. It is now time to execute remote queries as it is actual purpose of all these configurations. Distributed queries reference one or more linked servers. And perform read or update operations against remote tables, views, or stored procedures. The types of query operations that are supported against linked servers depend on the level of support for transactions present in the OLE DB providers used. The basic syntax for referencing a linked server is using a four-part name. To fetch data from pubs.authors of dbatest server
SELECT * FROM [dbatest].pubs.dbo.authors GO
To execute a system-stored procedure on the linked server
EXEC [dbatest].master.dbo.sp_monitor GO
Tired of using four parts name?
If it is not convenient to use four parts lengthy name then you may create a synonym for whole name. To create a synonym mySyn for whole four parts name
CREATE SYNONYM testSynonym FOR [dbatest].pubs.dbo.authors GO
It is relevant to mention here that SQL Server provides another way to execute distributed queries other than using the four parts naming method. OPENQUERY is a function that issues a pass-through query against an existing linked server and is referenced in the FROM clause of a query just like a table. The syntax is as follows:
OPENQUERY ( linked_server ,'query' ) GO
Now it is time to see that how to drop the created objects like linked servers, login mappings and synonyms.
--To drop the mapped login that is in our case dba sp_droplinkedsrvlogin 'dbatest' , 'dba' GO --To drop the linked server dbatest sp_dropserver 'dbatest' GO --To directly drop linked server along with all mapped logins sp_dropserver 'dbatest', 'droplogins' GO --To drop the synonym mySyn for linked server four parts name DROP SYNONYM testSynonym GO
Ad Hoc QUERIES USING OPENROWSET
In some cases it is not required to retain the connection from linked server for a long time. So to execute such Ad-hoc queries OPENROWSET command is used. It is referenced in the FROM clause and acts like a table in a SELECT statement. OPENROWSET creates an ad hoc connection to the data and does not use an existing linked server connection to query the remote data source.
This property to use OPENROWSET to query a remote data source is off by default in SQL Server 2005. You may turn it on through surface area configuration.
To perform same select operation from pubs.authors database on dbstest, we have following format of OPENROWSET
SELECT * FROM OPENROWSET('SQLNCLI','dbatest';'sa';'test', 'SELECT * from pubs..authors') GO