Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

119


Chapter 6: Introducing Basic Query Flow


6


[Table Name]
If the name of a database object, such as a table or column name, confl icts with a SQL
reserved keyword, you can let SQL know that it’s the name of an object by placing it inside
square brackets. The square brackets are specifi c to SQL Server and not part of the ANSI
SQL standard. For example, if you have a database (DB1) that contains a table named
[Order] you would be required to place the table name inside square brackets. The following
code illustrates this:

USE DB1;
SELECT OrderID, OrderDate
FROM [Order];

Although it’s an incredibly poor practice to include spaces within the names of database
objects, it is possible nevertheless. If this is the case, square brackets are required when
specifying the database object. Again, assuming you work in database DB1 and it contains
a table name Order Details, you would query the table using the following code:

USE DB1;
SELECT OrderID, ProductID, Quantity
FROM [Order Details];

The collation of the database determines its character set and sort order, but it can also affect object names within
SQL statements. If the collation is case-sensitive, then the object names must be case-sensitive as well. For exam-
ple, with a case-sensitive collation, a table created with the name Person is different from person or PERSON.
As a result, when referencing these objects in queries, you must adhere to any case-sensitivity specifi ed for that
object name.

Fully Qualifi ed Names
The full and proper name for a table is not just the table name but a fully qualified name,
sometimes informally referred to as the four-part name:

Server.Database.Schema.Table

If the table is in the current database, then the server and database name are not required,
so when SQL Server developers talk about a qualifi ed table name, they usually mean a two-
part table name:

Schema.Table

c06.indd 119c06.indd 119 7/30/2012 4:16:00 PM7/30/2012 4:16:00 PM


http://www.it-ebooks.info
Free download pdf