![peek synonym peek synonym](https://image.made-in-china.com/43f34j00szQfuZnEHJog/Peek-Fiber-Monofilament-1-75mm-for-3D-Printing.jpg)
Just as you can use a synonym to allow you to change the name of an object without your applications or users knowing the difference, you can also easily move an object to a different database (or even a different server) if you have your application and queries using the synonym for all queries involving that object.
![peek synonym peek synonym](https://en.bcdn.biz/Images/2020/11/10/feb9d05a-a5be-47c9-88ba-85ee62a6fd9a.jpg)
#Peek synonym code
I've even been involved with the design of a web application where we created a Synonym for every single table in the database and had the code use Synonyms exclusively. I've personally used this, and found that it works well. Using the example from above, when April 2014 comes around, you could drop the CurrentSalesData synonym, and create a new one pointing to tblSalesNbrsApr2014. Your queries and application can continue to simply use the Synonym to access that object, and will have no clue that its actual name has changed. Instead of hunting around and finding all references to that object and updating it, you simply need to drop and recreate the Synonym and you are good to go. This will allow you to change object names with a lot less effort. One way to ease the pain of renaming an object is to create a Synonym pointing to that object, and always use the Synonym in your queries from the beginning. This can prove to be a difficult tasks if you have a large number of queries or applications that refer to that object. It occasionally becomes necessary to rename SQL objects in your databases. Using "CurrentSalesData" instead of "2014." will make your queries easier to write and understand. You could simple create a synonym to point to that object, and then reference that synonym in your queries.įOR 2014 For example, you may have a need to use this crazy four-part name to get to a table: If you have a need in a query to reference a table on a remote server that you don't control, you may find the name to be unintuitive. One way they can be used is simply to shorten or clarify the names of objects with long or hard to decipher names. But how can they be used in practice to make our lives easier? Here are a few examples of how they can be used, some of which I have used myself.
#Peek synonym how to
Now we know what a synonym is and how to create one. See the links below in "References/Further Information" section for the Books Online articles for Synonyms. This will give you the name of the synonym, and the name of the object the synonym is created for. The sys.synonyms catalog view can be used to get a list of synonyms you have in a given database. You could then reference the synonym instead of the remote object:įROM The object you are creating the synonym for does NOT have to actually exist at the time the synonym is created.įor example, if you wanted to create a synonym to point to a customers table on another server, you would do so as follows:įOR You can create synonyms for Stored Procedures, Functions, Tables (even temp tables!), and Views. The following syntax is used to create a synonym: It sounds really simple, but if you think about it, this can be very useful for DBAs and developers if applied carefully. This article discusses what exactly a synonym is, and a few examples of how to use them, some of which I have used myself with success.Ī synonym, like the name implies, is nothing more than an alternate name you create for another database object. Synonyms are very simple database objects, but have the potential to save a lot of time and work if implemented with a little bit of thought. The concept of a synonym was introduced in SQL Server 2005.