Monday, 11 June 2012

SQL Linked Server and Distributed Server


Microsoft SQL Server 2008 provides numerous features for integrating SQL Server databases with other SQL Server databases and with other data sources, including distributed data, linked servers, and replication. Distributed data includes support for distributed queries, distributed transactions, and remote stored procedure execution. These distributed data features are handled through linked servers, which can be computers running SQL Server or computers running other database server software, such as Oracle DB.

Distributed Queries

When you execute a distributed query, SQL Server interprets the command and then breaks it down for the destination OLE DB provider using rowset requests. A rowset is a type of database object that enables OLE DB data providers to support data with a tabular format. As their name implies, rowset objects represent a set of rows and columns of data. After creating the rowset objects, the OLE DB provider calls the data source, opens the necessary files, and returns the requested information as rowsets. SQL Server then formats the rowsets as result sets and adds any applicable output parameters.
You can create simple distributed queries quickly by making your own rowsets. To do this, you use the Openrowset function. When you use this function, you do not need to use linked servers, and you can use the Openrowset function in place of a table in a query if you pass parameters that identify the OLE DB data source and provider.
You use the Openrowset function in the same way that you use virtual tables; simply replace the virtual table reference with an Openrowset reference. shows the syntax and usage of Openrowset.
Syntax for SELECT with Table Alias

SELECT selection FROM OPENROWSET(rowset_options) AS table_alias

Syntax for OPENROWSET

OPENROWSET
( { 'provider_name' , { 'datasource' ; 'user_id' ; 'password'
   | 'provider_string' }
      , { [ catalog. ] [ schema. ] object
   | 'query' }
   | BULK 'data_file' ,
       { FORMATFILE = 'format_file_path' [ <bulk_options> ]
       | SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB }
} )

<bulk_options> ::=
   [ , CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' }]
   [ , ERRORFILE = 'file_name' ]
   [ , FIRSTROW = first_row ]
   [ , LASTROW = last_row ]
   [ , MAXERRORS = maximum_errors ]
   [ , ROWS_PER_BATCH = rows_per_batch ]
   [ , ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) [ UNIQUE ]

Usage

USE pubs
GO
SELECT a.*
FROM OPENROWSET('SQLOLEDB','DBname';'username1';'pwd@123',
'SELECT * FROM pubs.dbo.authors ORDER BY au_lname, au_fname')
AS a


Linked Queries

You also can use remote stored procedures that execute against the linked server or distributed queries that access tables in the linked server through SELECT, INSERT, UPDATE, and DELETE statements that reference the linked server using a four-part name. The syntax for a four-part name is
Format : LinkedServerName.DatabaseName.TableName.ColumnName

In this example, LinkedServerName is the name of a previously defined linked server, DatabaseName is the name of a database, TableName is the name of a database table, and ColumnName is the name of a table column, such as

SELECT * FROM lnkName.Customers.SalesMan.EmployeeID
 
Syntax

sp_addlinkedserver [@server =] 'server'
    [, [@srvproduct =] 'product_name']
    [, [@provider =] 'provider_name']
    [, [@datasrc =] 'data_source']
    [, [@location =] 'location']
    [, [@provstr =] 'provider_string']
    [, [@catalog =] 'catalog']

Usage
EXEC sp_addlinkedserver
        @server='ORADBSVR38',
        @srvproduct='Oracle',
        @provider='OraOLEDB.Oracle',
        @datasrc='ORACLE10';