Blog Post

The parts of an object’s name

,

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

Filed under: Microsoft SQL Server, SQLServerPedia Syndication, T-SQL Tagged: microsoft sql server, T-SQL

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating