• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Get Result from two diffrent databse

Hello Expert, i am trying to writ an sql statment that retrieve a data from two defrrent databse depend on a condition, will actully can i do this in sql??
i have a table that contain a user id i want to get the full name either from db1.table1 or db2.table1 my sql statement is

SELECT     dbo.tblCallDetail.UserId, dbo.tblMinisUser.FullName, pr1.dbo.GetUserDetailsE.EngName
FROM        
dbo.tblCallDetail INNER JOIN  
dbo.tblMinisUser ON dbo.tblCallDetail.UserId = dbo.tblMinisUser.UserId
AND
dbo.tblCallDetail.UserId = dbo.tblMinisUser.UserId INNER JOIN
pr1.dbo.GetUserDetailsE ON dbo.tblCallDetail.UserId = pr1.dbo.GetUserDetailsE.PERUserName AND
dbo.tblCallDetail.UserId = pr1.dbo.GetUserDetailsE.PERUserName

but its wrong sql how can i correct the sql?
0
AZZA-KHAMEES
Asked:
AZZA-KHAMEES
  • 2
  • 2
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
let*s try this:
SELECT  cd.UserId, mu.FullName, gud.EngName
FROM dbo.tblCallDetail cd
LEFT JOIN  dbo.tblMinisUser mu 
  ON cd.UserId = mu.UserId 
LEFT JOIN pr1.dbo.GetUserDetailsE gud
  ON cd.UserId = gud.PERUserName 

Open in new window

0
 
AZZA-KHAMEESAuthor Commented:
its working but can i show the fields mu.FullName, gud.EngName in one field called FullUserName ??
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no problem:
SELECT  cd.UserId, COALESCE( gud.EngName, mu.FullName ) FullUserName 
FROM dbo.tblCallDetail cd
LEFT JOIN  dbo.tblMinisUser mu 
  ON cd.UserId = mu.UserId 
LEFT JOIN pr1.dbo.GetUserDetailsE gud
  ON cd.UserId = gud.PERUserName 

Open in new window

0
 
AZZA-KHAMEESAuthor Commented:
Thank you :)
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now