Click to Rate and Give Feedback
MSDN
MSDN Library
SQL Server
SQL Server 2005
 CREATE SYNONYM (Transact-SQL)
Community Content
In this section
Statistics Annotations (1)
SQL Server 2005 Books Online (November 2008)
CREATE SYNONYM (Transact-SQL)

Creates a new synonym.

Topic link icon Transact-SQL Syntax Conventions

CREATE SYNONYM [ schema_name_1. ] synonym_name FOR < object >

< object > :: =
{
    [ server_name.[ database_name ] . [ schema_name_2 ].| database_name . [ schema_name_2 ].| schema_name_2. ] object_name
}
schema_name_1

Specifies the schema in which the synonym is created. If schema is not specified, SQL Server 2005 uses the default schema of the current user.

synonym_name

Is the name of the new synonym.

server_name

Is the name of the server on which base object is located.

database_name

Is the name of the database in which the base object is located. If database_name is not specified, the name of the current database is used.

schema_name_2

Is the name of the schema of the base object. If schema_name is not specified the default schema of the current user is used.

object_name

Is the name of the base object that the synonym references.

The base object need not exist at synonym create time. SQL Server checks for the existence of the base object at run time.

Synonyms can be created for the following types of objects:

Assembly (CLR) Stored Procedure

Assembly (CLR) Table-valued Function

Assembly (CLR) Scalar Function

Assembly Aggregate (CLR) Aggregate Functions

Replication-filter-procedure

Extended Stored Procedure

SQL Scalar Function

SQL Table-valued Function

SQL Inline-table-valued Function

SQL Stored Procedure

View

Table1 (User-defined)

1 Includes local and global temporary tables

Four-part names for function base objects are not supported.

Synonyms can be created, dropped and referenced in dynamic SQL.

To create a synonym in a given schema, a user must have CREATE SYNONYM permission and either own the schema or have ALTER SCHEMA permission.

The CREATE SYNONYM permission is a grantable permission.

ms177544.note(en-US,SQL.90).gifNote:
You do not need permission on the base object to successfully compile the CREATE SYNONYM statement, because all permission checking on the base object is deferred until run time.

The following example first creates a synonym for the base object, Product in the AdventureWorks database, and then queries the synonym.

USE tempdb;
GO
-- Create a synonym for the Product table in AdventureWorks.
CREATE SYNONYM MyProduct
FOR AdventureWorks.Production.Product;
GO

-- Query the Product table by using the synonym.
USE tempdb;
GO
SELECT ProductID, Name 
FROM MyProduct
WHERE ProductID < 5;
GO

Here is the result set.

-----------------------
ProductID   Name                      
----------- --------------------------
1           Adjustable Race
2           Bearing Ball
3           BB Ball Bearing
4           Headset Ball Bearings
(4 row(s) affected)

In the following example, the base object, Contact, resides on a remote server named Server_Remote.

EXEC sp_addlinkedserver Server_Remote;
GO
USE tempdb;
GO
CREATE SYNONYM MyEmployee FOR Server_Remote.AdventureWorks.HumanResources.Employee;
GO
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Synonyms vs. views      Yewtaah ... Thomas Lee   |   Edit   |  
Some work associates and I have been discussing the differences, pros, and cons of using synonyms as opposed to what we refer to as transparency views. For example if you have 2 databases, Accounting and HumanResources, you many want to reference data in one from the other and vice-versa. Typically, we have done this similar to the following:

--In Accounting
CREATE VIEW Employees as SELECT * FROM HumanResources.Employees

--In HumanResources
CREATE VIEW Expenses as SELECT * FROM Accounting.Expenses

This effectively accomplishes the same thing as a synonym with just slightly different syntax. However, my understanding is that synonyms are less subject to binding errors if the underlying table changes. Is this accurate? Any other reasons to use one vs. the other? Any performance implications?

[Noelle Mallory - MSFT] Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker