Skip to content
Advertisement

How to query across multiple databases

I have a set of data that belongs to two different tables in two different databases. I have performed the query but I am getting a syntax error.

The two databases are:

dbo.Serve and dbo.Back

This is what I am doing:

SELECT Back.[dbo].Back$.Last_refreshed, dbo.Back.Backup_Name, dbo.Back$.Fle, dbo.Back$.Free_Space_TB, dbo.Backup$.Operational_status, dbo.Serve$.Name,
       dbo.Back$.Rack_Enclosure, dbo.Back$.Server_Name, dbo.Back$.SIZE_TB, x.field, x.piv, dbo.Serve$.SKU,
CASE
           WHEN  dbo.Serve$.SKU like 'F1%' or dbo.Serve$.SKU like 'F2%' or dbo.Server_History$.SKU like 'F3%' or
           dbo.Serve$.SKU like 'FY14%' or dbo.Serve$.SKU like 'F5%' or dbo.Server_History$.SKU like 'F6%' then 'Group1'
           WHEN dbo.Serve$.SKU like 'Gr%' or dbo.Serve$.SKU like 'Fr%'  or dbo.Server_History$.SKU like 'Ex%'
           or dbo.Serve$.SKU like 'Ga%' or dbo.Serve$.SKU like 'Ho%' or dbo.Serve$.SKU like 'Gen%' then 'Group2'
END AS grp
FROM    dbo.Back$
inner join dbo.Serve$  ON dbo.Serve$.Name = dbo.Back$.Server_Name and dbo.Back$.Last_refreshed = dbo.Serve$.Last_refreshed
cross apply (values (dbo.Back$.Free_Space_TB, 'Available'), (dbo.Back$.SIZE_TB - dbo.Back$.Available, 'Used')) AS x(field, piv)

Why would it give me an error on this part?

 SELECT Back.[dbo].Back$.Last_refreshed,

as the syntax appears correct

Any help is appreciated.

Advertisement

Answer

This Back.[dbo].Back$.Last_refreshed, does not make sense. You can still use databasename.schemaname.columnname or a four part name (if you set up linked servers… instancename.databasename.schemaname.columnname), but it is not clear what you are referring to with: Back.[dbo].Back$.Last_refreshed

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement