{"id":28073,"date":"2022-12-20T12:37:03","date_gmt":"2022-12-20T02:37:03","guid":{"rendered":"https:\/\/database.guide\/?p=28073"},"modified":"2025-11-15T12:07:07","modified_gmt":"2025-11-15T02:07:07","slug":"how-to-create-a-synonym-in-sql-server-t-sql","status":"publish","type":"post","link":"https:\/\/database.guide\/how-to-create-a-synonym-in-sql-server-t-sql\/","title":{"rendered":"How to Create a Synonym in SQL Server (T-SQL)"},"content":{"rendered":"\n<p class=\"\">In SQL Server we can create synonyms, which allow us to provide an alternative name for a database object. Once created, we can reference the synonym instead of the object itself. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"\">One benefit of synonyms is that they can help shorten the code we need to use when referencing an object. For example, we can type the synonym name instead of the full qualified path when referencing objects in another schema, database, or server. <\/p>\n\n\n\n<p class=\"\">We can also use synonyms to protect client applications from changes made to the name or location of the base object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p class=\"\">Suppose we have a table called <code>Customers<\/code> in the <code>Sales<\/code> schema. We can normally access this table at <code>Sales.Customers<\/code>. However, we can also create a synonym for this table so that it can be accessed as <code>Customers<\/code>.<\/p>\n\n\n\n<p class=\"\">First, here&#8217;s what happens when I query that table without specifying the schema:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    CustomerId,\n    CustomerName\nFROM Customers;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Msg 208, Level 16, State 1, Line 1\nInvalid object name 'Customers'.<\/pre>\n\n\n\n<p class=\"\">This happened because I didn&#8217;t qualify the path with the schema name. When we don&#8217;t do that in a query, SQL Server first looks at our default schema, then it looks in the <code>dbo<\/code> schema. In my case, <code>dbo<\/code> is my default schema.<\/p>\n\n\n\n<p class=\"\">Let&#8217;s now create a synonym for the full path <code>Sales.Customers<\/code>, so that we can reference the table without qualifying it with the schema name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE SYNONYM Customers\nFOR Sales.Customers;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Commands completed successfully.<\/pre>\n\n\n\n<p class=\"\">Now we can run our query again, this time without error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    CustomerId,\n    CustomerName\nFROM Customers;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CustomerId  CustomerName      \n----------  ------------------\n1001        Palm Pantry       \n1002        Tall Poppy        \n1003        Crazy Critters    \n1004        Oops Media        \n1005        Strange Names Inc.\n\n5 row(s) returned<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Synonym to Another Database or Server<\/h2>\n\n\n\n<p class=\"\">We can create synonyms to objects located in other databases or on other servers. To do this, simply use the fully qualified name to the object on that database\/server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE SYNONYM Cats\nFOR PetHotel.dbo.Cats;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Commands completed successfully.<\/pre>\n\n\n\n<p class=\"\">That created a synonym to the <code>dbo.Cats<\/code> table in the <code>PetHotel<\/code> database. Now we can query that table without having to qualify it with the name of the database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM Cats;<\/code><\/pre>\n\n\n\n<p class=\"\">If we hadn&#8217;t created the synonym, we would have had to qualify the table with the schema and database name, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM PetHotel.dbo.Cats;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Synonyms for Non-Existent Objects<\/h2>\n\n\n\n<p class=\"\">It&#8217;s actually possible to create a synonym for an object that doesn&#8217;t yet exist. SQL Server doesn&#8217;t check that the base object exists when we create the synonym &#8211; it only checks for the existence of the base object at run time.<\/p>\n\n\n\n<p class=\"\">Therefore, we can do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE SYNONYM MyObject\nFOR NonExistentObject;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Commands completed successfully.<\/pre>\n\n\n\n<p class=\"\">In this case, <code>NonExistentObject<\/code> doesn&#8217;t actually exist, but that didn&#8217;t stop SQL Server from creating the synonym. However, we will get an error when we try to reference the synonym:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM MyObject;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Msg 5313, Level 16, State 1, Line 1\nSynonym 'MyObject' refers to an invalid object.<\/pre>\n\n\n\n<p class=\"\">As expected, we get an error that tells us that the synonym refers to an invalid object.<\/p>\n\n\n\n<p class=\"\">However, if we create the object we will no longer get the error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE VIEW NonExistentObject\nAS\nSELECT CustomerName FROM Customers;<\/code><\/pre>\n\n\n\n<p class=\"\">Now let&#8217;s try the query again:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM MyObject;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CustomerName      \n------------------\nPalm Pantry       \nTall Poppy        \nCrazy Critters    \nOops Media        \nStrange Names Inc.\n\n5 row(s) returned<\/pre>\n\n\n\n<p class=\"\">In my case, my synonym refers to a <a href=\"https:\/\/database.guide\/what-is-a-view\/\" data-type=\"post\" data-id=\"315\">view<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Valid Objects for Synonyms<\/h2>\n\n\n\n<p class=\"\">We can create synonyms for the following objects:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\">Assembly (CLR) stored procedure <\/li>\n\n\n\n<li class=\"\">Assembly (CLR) scalar function <\/li>\n\n\n\n<li class=\"\">Replication-filter-procedure <\/li>\n\n\n\n<li class=\"\">SQL scalar function <\/li>\n\n\n\n<li class=\"\">SQL inline-tabled-valued function <\/li>\n\n\n\n<li class=\"\">View Assembly (CLR) table-valued function <\/li>\n\n\n\n<li class=\"\">Assembly (CLR) aggregate functions <\/li>\n\n\n\n<li class=\"\">SQL table-valued function <\/li>\n\n\n\n<li class=\"\">SQL stored procedure <\/li>\n\n\n\n<li class=\"\">Table, including local and global <a href=\"https:\/\/database.guide\/what-is-a-temp-table\/\" data-type=\"post\" data-id=\"46127\">temporary tables<\/a> (User-defined)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">More Information<\/h2>\n\n\n\n<p class=\"\">See the <a rel=\"noreferrer noopener\" href=\"https:\/\/learn.microsoft.com\/en-us\/sql\/relational-databases\/synonyms\/synonyms-database-engine\" data-type=\"URL\" data-id=\"https:\/\/learn.microsoft.com\/en-us\/sql\/relational-databases\/synonyms\/synonyms-database-engine\" target=\"_blank\">Microsoft documentation<\/a> for more information about synonyms such as permissions, where they can be used, etc.<\/p>\n\n\n\n<p class=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL Server we can create synonyms, which allow us to provide an alternative name for a database object. Once created, we can reference the synonym instead of the object itself.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[10,77,61],"class_list":["post-28073","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-how-to","tag-mssql","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=28073"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28073\/revisions"}],"predecessor-version":[{"id":46216,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/28073\/revisions\/46216"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=28073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=28073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=28073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}