Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2007, 01:12 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default Aliasing a database

OK, sorry for all the posts. I am trying to find the best way to accomplish a specific task and I keep hitting some walls.

I got the linked server thing to work, and this question is somewhat related to that. Is there any way to "alias" a database? Let me explain...

Our application has two databases. The main "production" database and an archive database. We have a process in which database is "archived" from the production db to the archive db. However, we can't always guarantee that the archive db will always be called the same thing.

So, we want to be able to Alias it so that the code in the production db never needs to be changed.

Does this make sense?

Any input is greatly appreciated...

Scott Klein
Author - Professional SQL Server 2005 XML
http://www.wrox.com/WileyCDA/WroxTit...764597922.html
__________________
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
 
Old February 14th, 2007, 01:47 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

So, the answer is a combination of both Linked Servers and Synonyms. Synonyms allow you to "alias" database objects, even objects in other databases, AND databases on other servers.

To alias a db object in a database on the same machine, the syntax is real easy:

CREATE SYNONYM aliasname FOR database.dbo.objectname
GO
SELECT * FROM aliasname

If you want to alias an object on another server, you first need to create a LINKED SERVER then create the synonym using the Linked Server name.

CREATE SYNONYM aliasname FOR linkedservername.database.dbo.objectname
GO
SELECT * FROM aliasname

Pretty cool...


Scott Klein
Author - Professional SQL Server 2005 XML
http://www.wrox.com/WileyCDA/WroxTit...764597922.html
 
Old February 18th, 2007, 01:05 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Sorry for the late reply...

SYNONYM will certainly work for SQL Server 2005... if you only have SQL Server 2000, a view works very well and has some other advantages, as well. For example, as dumb as it sounds, some of our folks need the ability to work with current information AND the archived information at the same time. Rather than them doing a JOIN in all their code, we simply made a view that does the join. For example, we have a current table called InvoiceDetail and an archived table (for things more than 120 days old) also called InvoiceDetail but in a different database. We called the view "InvoiceDetailALL".

This method will allow you to split voluminous data into online, near-line, and "almost off-line" databases and make changes in the background without having to change a lick of foreground code... Can't necessarily do that even with a synonym... unless of course, the synonym refers to a view :D

--Jeff Moden
 
Old February 18th, 2007, 04:47 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Thanks for the reply Jeff. Luckily we're using SQL Server 2005. However, you're solution may also come in handy if we aren't achieving the results we expect via SYNONYMS.

Scott Klein
Author - Professional SQL Server 2005 XML
http://www.wrox.com/WileyCDA/WroxTit...764597922.html





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem to restore database in C#2005 database acmuralee MySQL 0 March 25th, 2008 04:42 AM
Access database database format itHighway Access 0 February 24th, 2008 06:34 PM
Microsoft JET Database Database Engine (0x80040E09 cannielynn0312 Classic ASP Professional 2 December 17th, 2007 02:50 AM
Copying Table From one Database To Anoter Database jayanth_nadig VB Databases Basics 1 June 19th, 2006 02:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.