I have a two tables TabA and TabB. TabA is in DB_A and TabB in 'DBX'_B.
Now I need to get the DBX_B name from a field from TabA of DBA and join them in a query to pull the data from both these tables.
DBA.TabA:
ID DB_Name UserName Password
-------------------------------------------
101 DBX xyz abc
DBX_B.TabB:
ID Type FirstName LastName
-------------------------------------------
101 Admin xyz abc
I need to pull ID, Username, Password
from DBA.TabA
and pull Type, Firstname, LastName
from DBX_B.TabB
. But the 2nd database name to use can be identified from DB_NAME and concat it with string like _B'. So the 2nd database to pull from is
DBA.TabA.DB_Name' + _B
.
Join these two tables on ID from both.
The query can look something like:
SELECT DBA.TabA.ID, DBA.TabA.Username, DBA.TabA.Password,
DB2.TabB.Type, DB2.TabB.FirstName, DB2.TabB.Lastname
FROM DBA, CONCAT(DBA.TabA.DB_Name, '_B') as DB2
WHERE DBA.TabA.ID = DB2.TabB.ID
Of course, we can use Join too instead of where.
Is something like this possible? Ideas?