This article helps you to understand the concept of Linked Servers in SQL Server.
I will show with an example, how to connect the two different databases using Linked Servers in SQL Server 2008, 2012 step by step.
What is Linked Servers or Database Links?
Don't be confused by the two terms; both are the same. In SQL Server it is called a Linked Server whereas in Oracle it's DBLinks (Database Links).
Linked Servers allows you to connect to other database instances on the same server or on another machine or remote servers.
It allows SQL Server to execute SQL scripts against OLE DB data sources on remote servers using OLE DB providers.
The remote servers can be SQL Server, Oracle etc. which means those databases that support OLE DB can be used for linking servers.
After setting up the Linked Servers we can easily access the other server tables, procedures etc.
Once the connection is established, we can start with the CRUD operations.
The advantage is about security; it works on Windows as well as SQL Server Authentications.
How to find information about existing Linked Servers?
We can get Linked Server basic information by executing the following stored procedure created in the master database or default system.
- exec sp_linkedservers
Or using
- select * from sys.servers
You can get more columns and other information from this msdn article.
How to setup Linked Servers?
This can be done in two ways.
1. Using Transact-SQL
Using the following syntax we can create a Linked Server.
Example
This will create a Linked Server; we can view this from Management Studio.
Or by executing the following stored procedure, exec sp_linkedservers.
Now if want to see an entire SQL Script behind creation of a Linked Server, then from Management Studio right-click on:
LSNorthwind and Script Linked Server as -> CREATE To -> New Query editor window.
2. SQL Server Management Studio
Now by using SQL Server Management Studio we can create Linked Servers. I will show how to create connectivity with Oracle Database.
First of all we need to open telnet ports of Oracle Database on SQL Server. This will help to create communication between two different servers.
If an Oracle OLE DB provider not installed, then download and install.
Otherwise if the oracle client is already installed, then the driver is also updated.
Step 1
Open SQL Server Management Studio; go to Server Objects -> Linked Server.
Under Linked Server node we have Providers node that already having installed provides and that mapped to SQL Server.
Now right click on Linked Server node and click on New Linked Server which will open a new window for setup as below.
The new window contains following listing:
- Linked Server -> Its Linked Server name which needs to be created.
- Server Type -> It can be SQL Server or other data sources.
- Provider -> Will list all the installed providers.
- Product Name, e.g. Oracle, SQL Server.
- Data source, e.g. Oracle Database data source or other provider data source.
- Provider String or connection string, optional
- Catalog is database name, optional
Select Oracle Provider for OLE DB from Provider list and enter Data source information. This can be your Oracle database server IP with port name.
E.g. Data source -> 1.1.1.1:1521 or data source name. Here we can provide the whole connection string with user name and password and avoid updates in the Security tab. Enter Database name under Catalog entry.
Step 3
Or, instead of updating the provider string under the General tab, we can provide login credential details also under the Security tab at the top left corner.
Step 4
Under the Server Options tab we can update Connection Timeout, query execution timeout etc.
Now click on OK; this validates the entries and the Linked Server is created. If validation fails then an error window will be opened with error information.
Note: How to find connection string or data source information, refer to the following article.
How to do CRUD operations using Linked Server?
By using the OPENQUERY function we can execute the specified pass-through queries on the specified Linked Server and return the output.
Sample Select Syntax
Call a function or Stored Procedure
Insert records
To insert records we need to first select those columns to which records are to be inserted and pass the values through the select statement.
Update records
To update records we need to first select those columns to which records are to be updated and pass the values through the select statement with column names. We can have multiple column names separated with a comma.
- SELECT * FROM OPENQUERY(LSNorthwind, 'select * from dbo.Categories')
- SELECT * FROM OPENQUERY(LSNorthwind, 'EXEC [dbo].[CustOrdersOrders] VINET')
- insert OPENQUERY(LSNorthwind, 'select CategoryName, Description from dbo.Categories')
- select 'Testing', 'Testing'
- update OPENQUERY(LSNorthwind, 'select CategoryName from dbo.Categories where CategoryID=10')
- set CategoryName = 'New Test'
Delete records
We just need to select those records to be deleted and execute the delete statement.
Using Dynamic Queries
- delete OPENQUERY(LSNorthwind, 'select * from dbo.Categories where CategoryID in (9, 10, 11)')
Drop a Linked Server
This article was about creating Linked Servers in SQL Server 2008, 2012 and samples shows setting up connection with Oracle Server by selecting Oracle OLE DB provider with login credentials. Hope this article helped, please rate the article and post your comments.
- EXEC sp_dropserver 'LSNorthwind'
Conclusion
Post your queries to our Forum sections.
Thank You!

Suraj SorenPosted May 14, 2020, 6:27 AM
I was looking for this for a long time finally got this, very nice article.
Sr KarthigaPosted Jul 7, 2016, 6:45 AM
Nice explanation
Sr KarthigaPosted Jul 7, 2016, 6:44 AM
Good one
Madiha ZaibPosted Jan 28, 2013, 1:08 AM
Hello , Please Help me , I am using Access as the Linked Server in sql 2008, i want to update the record as using the query : UPDATE OPENQUERY([ACCESSDB],'Select [Phone2] from [Clients] where [Clientscode]=''ABL01''') SET [Phone2] = '12345678' but it will generate the error as: OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ACCESSDB" returned message "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.". OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ACCESSDB" returned message "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.". Msg 7320, Level 16, State 2, Line 1 Cannot execute the query "Select [Phone2] from [Clients] where [Clientscode]='ABL01'" against OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ACCESSDB". The provider could not support an interface required for the UPDATE/DELETE/INSERT statements.The provider indicates that conflicts occurred with other properties or requirements. Please Help Me......
Suresh SankarPosted Jan 21, 2013, 8:29 AM
Nice article...could that be possible to update the XML column through Linked server concept? for instance... UPDATE OPENQUERY ([serverA],'SELECT sSummarized,cast(xmlXtra as varchar)xmlXtra <DBNAME>.dbo.<TABLENAME> WHERE sSummarized IS NULL') SET sSummarized='DONE',xmlXtra = '<XmlXtra Complete = "DONE"/>'
sandeep beditedPosted Nov 23, 2012, 4:09 AMEdited Nov 23, 2012, 4:10 AM
Nice Article..Really good one..
Lajapathy ArunPosted Jun 23, 2012, 3:40 PM
Nice, looks like advance article. Happy to see new things :)
AndrewPosted Aug 1, 2011, 12:18 AM
It's very good article and the presentation is nice too.
Dea SaddlerPosted Jul 27, 2011, 2:22 AM
Helpful Article