简体   繁体   中英

Error when left joining two databases (SQL Server)

I have a great problem with joining two MSSQL databases together (on the same server) using LEFT JOIN. I run this SQL from the database OLAB_DB and get the error:

The multi-part identifier "OLAP_DB.dbo.OLAP_invoice.UserID" could not be bound.

There seem to be a problem with the DB not being able to find itself, and I have no idea of how to solve this. I have double and triple checked the spelling and rewrote the SQL several times, but now I just have to give up and ask for help :(

This doesn't work:

SELECT TOP 200 
    COALESCE(LTRIM(RTRIM(contact_db.dbo.ContactTable.EmailAdr)),LTRIM(RTRIM(contact_db.dbo.CustomerTable.EmailAdr))) AS EMAIL,
    OLAP_invoice.OrdreLinjeID AS ORDERNO
    OLAP_invoice.SalgsPris AS PRICE,
    OLAP_invoice.UserID AS CONTACTID
FROM OLAP_invoice,contact_db.dbo.CustomerTable
    LEFT JOIN contact_db.dbo.ContactTable 
    ON OLAP_DB.dbo.OLAP_invoice.UserID = contact_db.dbo.ContactTable.UserID
WHERE contact_db.dbo.CustomerTable.ClientID = OLAP_invoice.ClientID

But skipping the left join and just getting data from the two different databases works just fine.

This works just fine:

SELECT TOP 200 
    LTRIM(RTRIM(contact_db.dbo.CustomerTable.EmailAdr)) AS EMAIL,
    LTRIM(RTRIM(contact_db.dbo.ContactTable.UserID)) AS EMAIL2,
    OLAP_invoice.OrdreLinjeID AS ORDERNO
    OLAP_invoice.SalgsPris AS PRICE,
    OLAP_invoice.UserID AS CONTACTID
FROM OLAP_invoice,contact_db.dbo.CustomerTable
WHERE contact_db.dbo.CustomerTable.ClientID = OLAP_invoice.ClientID
AND contact_db.dbo.ContactTable.UserID = OLAP_invoice.UserID

The reason I need the LEFT JOIN, is because some orders are not registered with a UserID (only ClientID). I have checked the access rights and there is no problem to access the fields individually, the problem occurs when I have to compare the values in the LEFT JOIN and specifies the absolute path.

Do you have any idea of what can be wrong?

You can try this:

SELECT TOP 200   COALESCE(LTRIM(RTRIM(contact_db.dbo.ContactTable.EmailAdr)),LTRIM(RTRIM(contact_db.dbo.CustomerTable.EmailAdr))) AS EMAIL,
        OLAP_invoice.OrdreLinjeID AS ORDERNO
        OLAP_invoice.SalgsPris AS PRICE,
        OLAP_invoice.UserID AS CONTACTID
FROM OLAP_invoice LEFT JOIN contact_db.dbo.ContactTable ON OLAP_DB.dbo.OLAP_invoice.UserID = contact_db.dbo.ContactTable.UserID, contact_db.dbo.CustomerTable
WHERE contact_db.dbo.CustomerTable.ClientID = OLAP_invoice.ClientID

You are selecting from OLAP_invoice , but joining to OLAP_DB.dbo.OLAP_invoice . You need to qualify the table exactly the same every time, or use an alias. So either:

FROM OLAP_DB.dbo.OLAP_invoice
left join contact_db.dbo.ContactTable
on OLAP_DB.dbo.OLAP_invoice...

OR

FROM ROM OLAP_invoice
LEFT JOIN ontact_db.dbo.ContactTable
ON OLAP_invoice...

Or you could use an alias as well, less typing that way.

FROM OLAP_invoice OI
LEFT JOIN ontact_db.dbo.ContactTable CT
ON OI...

Personally, I try to avoid having multiple tables in my FROM without intentionally specifying the type of join.

I find something like this to be much more readable and maintainable :

SELECT TOP 200 
    COALESCE( LTRIM(RTRIM(con.EmailAdr)), LTRIM(RTRIM(cus.EmailAdr)) ) AS EMAIL,
    i.OrdreLinjeID AS ORDERNO
    i.SalgsPris AS PRICE,
    i.UserID AS CONTACTID

FROM OLAP_invoice i
    LEFT JOIN contact_db.dbo.CustomerTable cus ON cus.ClientID = i.ClientID
    LEFT JOIN contact_db.dbo.ContactTable con ON con.UserID = i.UserID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM