Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » SQL Server 2005/2008 » Distributed databases in SQL Server 2005.

Distributed databases in SQL Server 2005.

An increasing number of applications require access to multiple databases located at different sites. A distributed database system allows applications to access database locally and remote transparently.

Author Rank:
Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads :
Total page views :  8628
Rating :
 0/5
This article has been rated :  0 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
ArticleAd
Become a Sponsor



Introduction

An increasing number of applications require access to multiple databases located at different sites, perhaps widely separated geographically.

A common business scenario is a retailer with a network of warehouses spread across the country in order to speed delivery of its products. A distributed database system allows applications to access database local and remote transparently.

When data might be distributed? Why not gather all data of the enterprise in one central site? There are answers to these questions:

  • Data might be distributed in order to balance the workload.
  • Data might be on the site where it was created for maintenance and security purposes.
  • A client sends query to the closest database in order to minimized communication costs and increase throughput.
  • In order to increase data availability in the event of system crashes.

It is remarkable to say that there are two main approaches to distributed data: replication and distribution of queries. In this article, I will cover on distribution of queries where each system manages a single copy of data, and uses distributed transaction mechanisms to access local and remote data. In this case, the distributed database looks like a collection of individual databases with its own schema. If the database systems are from different vendors, the system is referred to as heterogeneous, otherwise homogeneous.

Implementing distributed databases using SQL Server 2005

Linked servers provide SQL Server the ability to access data from remote data sources. Using these mechanisms, you can issue queries, perform data modifications and execute remote procedures. You can use the T-SQL function OPENROWSET to query a remote data source without a linked server.

Let us now create a linked server to another SQL Server instance using the T-SQL procedure sp_addlinkedserver. The syntax of sp_addlinkedserver is shown in Listing 1:

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

Listing 1

And each parameter is described in table 1.

Parameter Description
server  Local name used for the linked server.
product_name  Product name of the OLE DB data source. For SQL Server instances, the product_name is 'SQL Server'.
provider_name This is the unique programmatic identifier for the OLE DB provider. When not specified, the provider name is the SQL Server data source. The explicit provider_name for SQL Server is SQLNCLI (for Microsoft SQL Native Client OLE DB Provider). MSDAORA is used for Oracle, OraOLEDB.Oracle for Oracle versions 8 and higher, Microsoft.Jet.OLEDB.4.0 for MS Access and MS Excel, DB2OLEDB for IBM DB2, and MSDASQL for an ODBC data source.
data_source This is the data source as interpreted by the OLE DB provider.
location The location interpreted by the OLE DB provider.
provider_string The connection string specific to the OLE DB provider.
catalog This varies from the database systems.

Table 1.

You can configure the linked server properties such as collection compatible, collation name, connect timeout and so on (for more information refer to the SQL Server online help) using the stored procedure sp_serveroption.

When executing a distributed query against a linked server, SQL Server maps your local login and credentials to the linked server. Based on the security on the remote data source, your credentials are accepted or rejected. By default, when a linked server is created, your local credentials are used to access data on the remote site. If you do not have the proper permission on the remote site, then your connection attempt is rejected. You can set up the mapping from your local login to any existing remote login using the stored procedure sp_addlinkedsrvlogin.

The syntax of this procedure is shown in Listing 2.

sp_addlinkedsrvlogin [ @rmtsrvname = ] 'rmtsrvname'
[ , [ @useself = ] 'useself' ]
[ , [ @locallogin = ]
'locallogin' ]
[ , [ @rmtuser = ]
'rmtuser' ]
[ , [ @rmtpassword = ]
'rmtpassword' ]

Listing 2.

The parameters of this procedure are described in the Table 2. 

Parameter Description
Rmtsrvname The linked server name.
Useself When the value true is used, the local SQL or Windows login is used to connect to the remote server name. If false, the locallogin, rmtuser, and rmtpassword parameters of the sp_addlinkedsrvlogin stored procedure will apply to the new mapping.
locallogin The name of login to be mapped. If this parameter is null, then the mapping applies to all the logins.
rmtuser The name of the remote user.
rmtpassword The password of the remote user.

Table 2.

Let us create and configure a linked server from one instance of SQL Server 2005 to one instance of SQL Server 2000 in order to execute queries on the sample database Northwind. The local and remote login mapping is done using Integrated Security associated to the current logged user. The code in TSQL is illustrated in Listing 2.

USE [master]

GO

EXEC master.dbo.sp_addlinkedserver @server = N'REMOTE_NORTHWIND', @provider='SQLNCLI', @srvproduct=N'', @datasrc='remote_site_dns_or_ip', @provstr='Integrated Security=SSPI;'

GO

EXEC master.dbo.sp_serveroption @server=N'REMOTE_NORTHWIND', @optname=N'collation compatible', @optvalue=N'false'

GO

EXEC master.dbo.sp_serveroption @server=N'REMOTE_NORTHWIND', @optname=N'data access', @optvalue=N'true'

GO

EXEC master.dbo.sp_serveroption @server=N'REMOTE_NORTHWIND', @optname=N'rpc', @optvalue=N'false'

GO

EXEC master.dbo.sp_serveroption @server=N'REMOTE_NORTHWIND', @optname=N'rpc out', @optvalue=N'false'

GO

EXEC master.dbo.sp_serveroption @server=N'REMOTE_NORTHWIND', @optname=N'connect timeout', @optvalue=N'0'

GO

EXEC master.dbo.sp_serveroption @server=N'REMOTE_NORTHWIND', @optname=N'collation name', @optvalue=null

GO

EXEC master.dbo.sp_serveroption @server=N'REMOTE_NORTHWIND', @optname=N'query timeout', @optvalue=N'0'

GO

EXEC master.dbo.sp_serveroption @server=N'REMOTE_NORTHWIND', @optname=N'use remote collation', @optvalue=N'true'

GO

USE [master]

GO

EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'REMOTE_NORTHWIND', @locallogin = NULL , @useself = N'true'

GO

Listing 3.

Now let us execute some queries on the remote instance, specifically on the database Northwind. You can issue distributed queries referring to linked server name by using the following syntax as shown in Listing 3.

[linked_server_name].[catalog].[schema].[object_name]

Listing 3.

Let us query the remote table Categories in the Northwind database using the connection provided by the linked server as shown in Listing 4.

select * from REMOTE_NORTHWIND.Northwind.dbo.Categories

Listing 4.

As you can see you have long name to refer to remote database object. SQL Server 2005 provides a mechanism named synonym to referring to long name. Let us create a synonym to the remote table as shown in Listing 5.

create synonym dbo.remote_northwind_categories for REMOTE_NORTHWIND.Northwind.dbo.Categories;

Listing 5.

Now the query in the Listing 4 is transformed into the query shown in Listing 6.

select * from dbo.remote_northwind_categories

Listing 6.

Conclusion

In this article I covered the main approach of distributed database and how it is realized using SQL Server 2005 systems.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
John Charles Olamendy
He’s a senior Integration Solutions Architect and Consultant. His primary area of involvement is in Object-Oriented Analysis and Design, Database design , Enterprise Application Integration, Unified Modeling Language, Design Patterns and Software Development Process. He has knowledge and extensive experience in the development of Enterprise Applications using Microsoft.NET and J2EE technologies and standards. He is proficient with distributed systems programming; and business-process integration and messaging using the principles of the Services Oriented Architecture (SOA) and related technologies such as Microsoft BizTalk Server, Web Services (Windows Communication Foundation, WSE, BEA WebLogic, Oracle AS and Axis) through multiple implementations of loosely-coupled system. He’s a prolific blogger contributing to .NET and J2EE communities and actively writes articles on subjects relating to integration of applications, business intelligence, and enterprise applications development. He holds a Master’s degree in Business Informatics at Otto Von Guericke University, Magdeburg, Germany. He was recently awarded as MVP. He currently works in the telecommunication industry and delivers integration solutions for this industry. He harbors a true passion for the technology.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved