Free SQL Server Learning - Using SQL Server DMVs to Help Improve Performance
solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups SQL Server Events I am MSSQLTips MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Pinterest Page MSSQLTips Twitter Page MSSQLTips Google+ Page














































SQL Server Four part naming

By:   |   Read Comments   |   Related Tips: More > T-SQL

Problem
With some applications there may be a need to pull data from other instances of SQL Server or from other databases within the same instance.  Often this can be achieved by having multiple connections from your application pointing to each of these data sources. This is great for this one application, but what if there is a need to do this within SQL Server or for stored procedures or views that are called from several applications?

Solution
SQL Server offers the functionality to reference objects within the database you are working or to reference objects in another database or even a different instance of SQL Server.  This is referred to as four-part naming.  The reason for this name is that there can be four parts that are used to reference the object as the following shows:

For SQL Server 2000
server.database.owner.object

For SQL Server 2005
server.database.schema.object

Object
In most cases, your SQL Server code probably only references the object name such as:

SELECT * FROM sysobjects

Owner or Schema
To further qualify this we can also specify the owner or schema of the object:

SELECT * FROM dbo.sysobjects

Database
To take this a step further we can also reference the database that the object resides in such as:

SELECT * FROM master.dbo.sysobjects

Server
And last we can reference this table on a totally different server by specifying the server name:

SELECT * FROM test1.master.dbo.sysobjects

The ability to reference an object this way opens up several possibilities whether it be within the same database or on other databases or other servers.  One example is the use of the owner or schema part.  This alone allows you to have the same object name in your database several times and with the owner or schema reference you can now signify which object you actually want to use in your code.

Also, if you have a standard database that may be used by multiple databases, by using the database part you can now have multiple databases pull in this standard data by using the database part of the four-part name.

And lastly, you can even separate your data even further by using the fourth part of the name that references the server.  One thing to note is that you will need to setup linked servers in order to use this option.  More information about linked servers can be found here for SQL Server 2000 and for SQL Server 2005.  Another thing to note is that there may be some performance issues if you are linking servers across your LAN or WAN and also if you are pulling in a lot of data.

Next Steps

  • Take a look at your code to see if there are areas that could benefit from this four-part naming structure
  • Another option to look at to retrieve data from another server or data source is OPENROWSET
  • Look at the use of SYNONYMS in SQL Server 2005 to simplify four-part naming
  • Also, take a look at ownership chaining and security/permission issues


Last Update: 11/6/2006

About the author

Greg is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.

View all my tips


Print  
Become a paid author


Comments and Feedback:


Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email Notify for updates

Signup for our newsletter


Comments
*Enter Code refresh code


 
Sponsor Information
Find and fix SQL Server problems before they happen - SQL diagnostic manager now with predictive analysis!

SQL Developer Bundle: Cut out dull work with 12 tools for simpler, faster database development. Free trial

What grade do you think your SQL Servers get? Find out with a SQL Server Health Check consultant in the USA.

Spring Clean Your Data - Clean your global contact data with Melissa Data tools for SSIS. Download a free trial!

The SQL Server Security THREAT - It’s Closer Than You Think


Copyright (c) 2006-2013 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com