简体   繁体   中英

SQL JOIN OPENQUERY Assistance

Looking for advise on the best way to approach the following

TBL1 (Local SQL DB) where ID = varchar

TBL2 (Remote MYSQL DB) where ID = INT

WITH TBL1 as (
SELECT 
     [Results]  as 'ID'
 FROM [DB].[results]
),

TBL2 as (


select * from openquery(LINKEDSERVER,'select  ID, Name from DB')

) 

Select  
   TBL1.[ID],

   TBL2.[NAME] 
FROM [DB]
left outer JOIN TBL1 ON TBL1.ID = TBL2.ID

Tried a couple different methods like CAST/CONVERT, but ending up with either an error like "Conversion failed when converting the varchar value '12345 ' to data type int." or getting all NULLS for TBL2.

Attempted the following:

WITH TBL1 as 
( 
SELECT CAST(ISNULL([Results],'0') AS INT) as 'ID' 
FROM [DB].[results] 
), 
TBL2 as ( select * from openquery(LINKEDSERVER,'select ID, Name from DB') 
) 

Select TBL1.[ID], TBL2.[NAME] 
FROM TBL1 Left Outer join TB2 ON TBL1.ID = TBL2.ID

This results in "Conversion failed when converting the varchar value '12345 ' to data type int."

Also Attempted:

WITH TBL1 as ( SELECT [Results] as 'ID' FROM [DB].[results] 
), 

TBL2 as ( select * from openquery(LINKEDSERVER,'select CONVERT(ID, CHAR) AS ID, Name from DB') 
) 

Select TBL1.[ID], TBL2.[NAME] 
FROM TBL1 Left Outer join TB2 ON TBL1.ID = TBL2.ID 

This results in the TBL1.ID values being displayed, but all the TBL2.Name values are NULL

Option 1: Try converting the INT to varchar on the MySQL side of the house. Try to do it right in the sql statement you pass in to the LinkedServer.

WITH TBL1 as ( SELECT [Results] as 'ID' FROM [DB].[results] 
), 

TBL2 as ( select * from openquery(LINKEDSERVER,'select CONVERT(ID, CHAR) AS ID, Name from DB') 
) 

Select TBL1.[ID], TBL2.[NAME] 
FROM TBL1 Left Outer join TB2 ON TBL1.ID = TBL2.ID 

The other option is of course the reverse (if you want to join by INT)

WITH TBL1 as 
( 
SELECT CAST(ISNULL(TRIM([Results]),'0') AS INT) as 'ID' 
FROM [DB].[results] 
), 
TBL2 as ( select * from openquery(LINKEDSERVER,'select ID, Name from DB') 
) 

Select TBL1.[ID], TBL2.[NAME] 
FROM TBL1 Left Outer join TB2 ON TBL1.ID = TBL2.ID 

Your syntax doesn't look right. Where does the DB C come from? Isn't DB the schema name?

WITH TBL1 as (
SELECT 
     [Results]  as ID
 FROM [DB].[results]
),
TBL2 as (
    select * from openquery(LINKEDSERVER,'select  ID, Name from DB')
) 

Select  
   TBL1.[ID],
   TBL2.[NAME] 
FROM TBL2 left outer JOIN TBL1 ON try_cast(TBL1.ID as int) = TBL2.ID;

Appreciate everyone's help. This is what got this working:

WITH TBL1 AS (
    SELECT REPLACE(REPLACE(ID, CHAR(13), ''), CHAR(10), '') 
    FROM DB
    WHERE ISNUMERIC(REPLACE(REPLACE(ID, CHAR(13), ''), CHAR(10), '')) = 1

),
TBL2 AS (
    SELECT *
    FROM OpenQuery(
        LINKEDSERVER,
        'SELECT id, name FROM DB'
    )
)
SELECT 
    TBL1.ID
    ,TB2.Name
FROM TBL2
LEFT OUTER JOIN TBL1 ON TBL1.ID = TBL2.id

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