February 4, 2008 at 10:54 am
Using select from two tables on two different databases
Hi all,
On Ms SQL server 2000 we have two databases 1.Prod with a table called A and 2. Dev with a table called B.
How do I run select statement from both databases?
Select prod.A.*, dev.B.* from prod, dev
Where prod.a.id = dev.b.id
The above select does not work.
Thanks for any help.:)
Abrahim
February 4, 2008 at 11:45 am
When you use two part naming (prod.A in your example) SQL thinks that "prod" is a reference to a schema. Use three part naming : [databasename].[schemaname].[tablename]
For your purposes, it's probably prod.dbo.A if the example tables are in the dbo schema.
February 4, 2008 at 12:09 pm
Just so we can all get into the right habits - use the three-part names like Matt Stockham pointed to, but do so only in the FROM clause, assigning an alias to the table.
So - in your example:
SELECT
ProdA.*,
DevB.*
FROM
Prod.dbo.A as ProdA
INNER JOIN Dev.dbo.B as DevB
ON ProdA.id=DevB.id
Anything over 2 parts used pretty much anywhere other than the FROM clause is now deprecated.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
February 4, 2008 at 12:18 pm
I tried:
select prod.dbo.A.*, dev.dbo.B.*
from prod, dev
where prod.dbo.A.id = dev.dbo.B.id
and prod.dbo.A.id <= 100
but getting error:
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'prod'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'dev'.
Thanks,:blush:
Abrahim
February 4, 2008 at 12:21 pm
That did it!!!
Thanks:D
April 2, 2008 at 9:08 am
Here is the real code:
select cp2abc.*
from prd2.dbo.abc as prd2abc, prd1.dbo.abc as prd1abc
where not exists
(select prd1abc.subj_num from prd1.dbo.abc as prd1abc where prd2abc.subj_num = prd1abc.subj_num
and prd2abc.abc_DATE = prd1abc.abc_date)
Abrahim:Whistling:
April 7, 2008 at 1:18 pm
In your query if you write like "prod.dbo.tablename" then SQL server take it like prod as ur datbase name,dbo as schema and tablename as table name you are trying to select.
so here it will take "prod" as your database name, not your server name...You need to specify linked server for that.
April 7, 2008 at 2:01 pm
In my example both database are on the same Server.
Abrahim
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply