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 2012 Forums
 Transact-SQL (2012)
 Query over 2 databases

Author  Topic 

kezzaman
Starting Member

3 Posts

Posted - 2013-07-31 : 03:02:12
Hello, I have made the following query, but I want to get the result of this query over 2 databases. These db's are on the same server and are from the same application. The other is an old db. I have search on the internet but I can't get it fixed. So if someone can help me i would be very pleased. Thank you.

DB names are DB_NEW and DB_OLD
Here is the query.


select
table1.itemcode,
table2.itemname,
MAX(table2.date) as last deliverydate

from
table1 inner join table2 ON table1.code = table2.code
group by table1.itemcode, table2.itemname

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-07-31 : 03:13:34
DB_NEW.dbo.table or DB_OLD.dbo.table


Too old to Rock'n'Roll too young to die.
Go to Top of Page

kezzaman
Starting Member

3 Posts

Posted - 2013-07-31 : 03:31:16
I want one result of the query over 2 DB's.
Because some items have no deliverydate in the new DB so in that cases it must show the date from the old DB.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-07-31 : 03:57:19
MAX( COAELESCE( table2.date, table1.date) ) as last_deliverydate


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -