Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Difference in Querying Data

Author  Topic 

cstokes91
Yak Posting Veteran

72 Posts

Posted - 2013-05-02 : 12:30:50
Hey,

I have a random question...

When you write a query... say SELECT* FROM [DatabaseName].[dbo].[TableName]

what is the difference in that and
[DatabaseName]..[TableName] ? I have seen it written like that before but I just don't want to run into errors later down the road for not fully qualifying or something.

I have even ran into weird errors from not putting the brackets around the database and table names.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-02 : 12:36:54
The 3-part naming convention is [Databasename].[SchemaName].[TableName]. When you omit the [Schemaname], it assumes the schema is the default schema (in your example dbo).

So most of the time, you could omit the schema name, but I always specify the actual schema name.
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2013-05-02 : 13:01:56
If you omit the schema name - it could cause performance issues. When you omit the schema - and you have multiple users using that query to access the system and each user has a different default schema, SQL Server will create a separate execution plan for each user.

Not only could you have multiple plans defined and saved in memory, but one user could end up with an optimal plan and a different user with a non-optimal plan and troubleshooting will become much harder.

I recommend that you always schema-qualify all objects and only use the database when accessing objects in another database.
Go to Top of Page
   

- Advertisement -