Tuesday, 25 December 2012

SQL SERVER – Introduction and Explanation to SYNONYM – Helpful T-SQL Feature for Developer


I learned about SYNONYM feature in SQL Server 2005.
DBA have been referencing database objects in four part names. SQL Server 2005 introduces the concept of a synonym. A synonyms is a single-part name which can replace multi part name in SQL Statement. Use of synonyms cuts down typing long multi part server name and can replace it with one synonyms. It also provides an abstractions layer which will protect SQL statement using synonyms from changes in underlying objects (tables etc).
Create Synonyms :
USE AdventureWorks;GOCREATE SYNONYM MyLocationFOR AdventureWorks.Production.Location;GO
Use Synonyms :
USE AdventureWorks;GOSELECT TOP *FROM MyLocation;GO
Drop Synonyms :
USE AdventureWorks;GODROP SYNONYM MyLocation;GO
Synonyms can be created on only following 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
  • Table (User-defined)
Additionally SYNONYMS can be used only to change data of object not the schema of the object. SYNONYMS can be used with only SELECT, UPDATE, INSERT, DELETE, EXECUTE commands.
Following is image demonstrates use of SYNONYMS.
An example of the usefulness of this might be if you had a stored procedure on a Users database that needed to access a Clients table on another production server. Assuming you created the stored procedure in the database Users, you might want to set up a synonym such as the following:
USE Users;GOCREATE SYNONYM ClientsFOR Offsite01.Production.dbo.Clients;GO
Now when writing the stored procedure instead of having to write out that entire alias every time you accessed the table you can just use the alias Clients. Furthermore, if you ever change the location or the name of the production database location all you need to do is modify one synonym instead of having to modify all of the stored procedures which reference the old server.

3 comments:

  1. Your style is very unique compared to other people
    I have read stuff from. Many thanks for posting when you've got the opportunity, Guess I'll just bookmark this web site.


    Also visit my web blog; best registry cleaner for windows 7

    ReplyDelete
  2. I do not even know how I stopped up here, but I thought
    this publish was great. I don't recognise who you are however certainly you're going
    to a well-known blogger in case you aren't already. Cheers!

    Feel free to surf to my blog: Buy Likes

    ReplyDelete
  3. Hey there! I could have sworn I've been to this site before but after browsing through some of the post I realized it's new to me.
    Anyhow, I'm definitely happy I found it and I'll be bookmarking and checking back frequently!


    my site :: buyfollowers365.Co.uk

    ReplyDelete