The parts of an object’s name

3

March 20, 2017 by Kenneth Fisher

Any object within a database in SQL Server has what is called a four part name. Which rather implies four pieces right? Simple enough

Server.Database.Schema.ObjectName

And you can find the lists of each of those pieces in

 
The breakdown is nice but how about usage? Let’s use an object in AdventureWorks2014 as an example.

One Part Name
vIndividualCustomer
This is just the name of the object. This is probably the most common usage and yet the only one I would recommend never using. (I’ll freely admit I’m not great at this myself btw.) When you only use a single name the schema is implied by the default schema of the user running the statement. That means that the same statement run by two different users could have two different meanings. Since most users have a default schema of dbo this would probably hit the object dbo.vIndividualCustomer. Assuming one even existed.

Two Part Name
Sales.vIndividualCustomer
This is the name of the object and the name of the schema. It specifically identifies an object within the database and for the reason stated above is what you should be using in most cases.

Three Part Name
AdventureWorks2014.Sales.vIndividualCustomer
Now we add the name of the database. You only really need this one if you are doing cross database queries or aren’t sure what context your query will be running under. In other words, if you are creating a script for someone else to use, and you aren’t sure if they are going to run it from master, or some userDB or even tempdb, then you had best use a three part name to call all of your objects. And if you are writing a query that references objects from two (or more) different databases at once (master and msdb for example) then you have to use a three part name for at least some of the objects (some, of course, can be from the current database).

Four Part Name
(local)\sql2016cs.AdventureWorks2014.Sales.vIndividualCustomer
Last but not least we add on the instance name. You’ll only need/see this if you are referencing an instance other than the one you are on. The most common (and only??) way being a linked server. You could use this in place of any of one, two, or three part names but there is really no point and most people don’t because of the extra typing.

Bonus
Some bonus knowledge. There is a great function called parsename that splits the name up by position.

PRINT parsename('(local)\sql2016cs.AdventureWorks2014.Sales.vIndividualCustomer'
	,1);
--vIndividualCustomer
PRINT parsename('(local)\sql2016cs.AdventureWorks2014.Sales.vIndividualCustomer'
	,2);
--Sales
PRINT parsename('(local)\sql2016cs.AdventureWorks2014.Sales.vIndividualCustomer'
	,3);
--AdventureWorks2014
PRINT parsename('(local)\sql2016cs.AdventureWorks2014.Sales.vIndividualCustomer'
	,4);
--(local)\sql2016cs

3 thoughts on “The parts of an object’s name

  1. […] enough right? You give it a name and point it at an object. You can use a one, two, three or even four-part name for the object you are pointing at. As best I can tell that object can be anything owned by a […]

  2. […] don’t need to use a three part name. SQL assumes you mean the current database if you don’t specify […]

  3. […] a query every object should be qualified. i.e. at least a two part name depending on the type of object and if it’s in the current database. This means that column […]

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 6,756 other subscribers

Follow me on Twitter

Archives

ToadWorld Pro of the Month November 2013