Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Dot table qualifier

Tags:

sql

sql-server

I am looking at a new database schema developed by an external vendor. There are two databases:

Database1
Database2

They have sent me an SQL statement that joins tables between the two databases. There are places where they have used a double dot notation. I have never seen this before. Please see the SQL statement below (this is not the statement they sent me):

select * from database2..Person

The statement above is run from database1. Why does it have two dots? If I remove one of the dots then the query does not run.

I have done some Googling and came across this: http://www.sqlservercentral.com/Forums/Topic585446-338-1.aspx. This suggests it is referring to the schema. However:

  1. The schema is empty in the sql statement i.e. there is no text in between the two dots.
  2. The Person table is part of the dbo schema in database2.
like image 234
w0051977 Avatar asked Jan 14 '16 09:01

w0051977


People also ask

What does 2 dots mean in SQL?

Double-dot notation follows this format: <NPS_DatabaseName>..<NPS_ObjectName> The two dots indicate that the schema name is not specified.

What is DBO in SQL?

The dbo schema is the default schema of every database. By default, users created with the CREATE USER Transact-SQL command have dbo as their default schema. The dbo schema is owned by the dbo user account. Users who are assigned the dbo as default schema don't inherit the permissions of the dbo user account.

What is colon in SQL?

The colon (:) is used to select "slices" from arrays. (See Section 5.12.) In certain SQL dialects (such as Embedded SQL), the colon is used to prefix variable names. The asterisk (*) has a special meaning when used in the SELECT command or with the COUNT aggregate function.


2 Answers

Thanks to this dot, the default schema (dbo) is choosen for your query.

When you have two databases it is required to give the full path to the table. If we have: Database1 schema: dbo, guest table dbo.A, guest: A Database2 schema: dbo, guest table dbo.B, guest: B

if we create select statement like:

select * from Database2..B

We are selecting data from dbo.B table IF we would like to specify schema we need to refer as

select * from Database2.schemaname.tablename

EDIT: As colleagues pointed out, the default schema can be changed in database, however in this particular example it seems to be dbo :)

like image 113
Rafał Czabaj Avatar answered Oct 17 '22 03:10

Rafał Czabaj


This is a database schema. Full three-part name of a table is:

databasename.schemaname.tablename

For a default schema of the user, you can also omit the schema name:

databasename..tablename

You can also specify a linked server name:

servername.databasename.schemaname.tablename

You can read more about using identifiers as table names on MSDN

like image 20
nano Avatar answered Oct 17 '22 01:10

nano