How and Why Should I Use SQL Server 2005 Synonyms
How and Why Should I Use SQL Server 2005 Synonyms
Problem
Changing the name of an table once an application has been deployed has
traditionally been a difficult task in SQL Server 2000. The typical solution in SQL
Server 2000 for referencing a different object was to use a View. A second option
was to use the sp_rename system stored procedure to rename objects as needed.
Unfortunately, this limits the capabilities of referencing other objects. The need to
be able to reference another object can become critical during system migrations,
application testing, data corruption, etc. Does SQL Server 2005 handle this in a
more elegant manner and support more than just tables?
Solution
SQL Server 2005 has introduced synonyms which enables the reference of another
object (View, Table, Stored Procedure or Function) potentially on a different server,
database or schema in your environment. In short, this means that the original
object that is referenced in all of your code is really using a completely different
underlying object, but no coding changes are necessary. Think of this as an alias as
a means to simplify migrations and application testing without the need to make any
dependent coding changes.
For more
information
visit - Using
Extended
Properties on
Database
Objects
USE [UserDefinedDatabase]
GO
CREATE SYNONYM [dbo].[Customer]
FOR [JTKNotebook].[JTKTest].[dbo].[Customer]
GO
EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'This is a
sample synonym referencing the JTKTest database' ,
@level0type=N'SCHEMA',@level0name=N'dbo',
@level1type=N'SYNONYM',@level1name=N'Customer'
How can I verify the statements issued against the synonym are affecting
the base object?
SELECT *
FROM [BaltSSUG].[dbo].[Customer]
SELECT *
FROM [JTKTest].[dbo].[Customer]
Next Steps