Solved

How do I join two databases when there is a "." in the table name.

Posted on 2009-05-19
8
153 Views
Last Modified: 2012-05-07
I am attempting to join two databases on the same sql server. I am able to access the databases like so for most of the tables:

SELECT s.item1,b.item2 FROM db1.dbo.t1 s join db2.dbo.t1 b on s.id1 = b.uid1

But when i need to access a view from db1 that contains a "." in the name of the actual table I get a sys.servers prompting me to add the server name. Example below:

SELECT s.item1,b.item2 FROM db1.dbo.name.v1 s join db2.dbo.t1 b on s.id1 = b.uid1

Unfortunately I cant change the name of the view "name.v1". Any help would be appreciated.
0
Comment
Question by:sbsnewbie
  • 5
  • 3
8 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
Hope this helps:

SELECT s.item1,b.item2 FROM db1.dbo.[name.v1] s join db2.dbo.t1 b on s.id1 = b.uid1
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
You can either use [name.v1] or "name.v1" to tell Server that it is a single word.
0
 

Author Comment

by:sbsnewbie
Comment Utility
I have tried both of these methods and I now get "invalid object name 'db1.dbo.name.v1'

If i insert a use db1 at the top of the query it works without the db1.dbo...but this wont work within the view I am trying to create in the db2.

Any thoughts?

0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
You can obtain an Invalid object name error in SQL Server due to the following reasons.

* the user you are connecting as does not have SELECT, UPDATE, INSERT, DELETE, EXEC permissions on the object;
* you aren't logging in / connecting as the user you expect;
* you are referencing the object without an owner name prefix (or with the wrong owner name);
* you are connected to the wrong database;
* you are, in fact, spelling the object's name incorrectly.

I suspect that you dont have SELECT permission on that view. Can you confirm whether you have SELECT permission on that view.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Accepted Solution

by:
sbsnewbie earned 0 total points
Comment Utility
Yes. When i operate this query everything is returned as expected:

SELECT item1 FROM name.v1

Its only when I attempt to join that table across two databases and I am forced to reference it as:

db1.dbo.name.v1 that it errors out.

Its OK, I just went to the source table of the view 'name.v1' and got the data I needed. The source table does not have a "." in the name of the table so this view was created successfully. Thanks for the feedback, still puzzled as to how I can access this correctly.
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
In your First Database db1, issue the below and check whether it works or not.

select * from db1.dbo.name.v1

If that fails, then issue the below

select * from db1.name.v1

If below one works, then from db2 issue the below statement which should work fine

select * from db1.name.v1
0
 

Author Comment

by:sbsnewbie
Comment Utility
First two queries did not work. I have tried all combinations without success.
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
Ok. Now provide error messages for each and every execution:

use db1
go

select * from db1.dbo.name.v1

If that fails, then issue the below

use db1
go

select * from db1.dbo.[name.v1]

If that fails, then issue the below

use db1
go

select * from db1.name.v1

If below one works, then from db2 issue the below statement which should work fine

use db2
go
select * from db1.name.v1
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now