2011-10-10 50 views
4

我想通過MS-SQL和MySql數據庫進行連接。如何在查詢中加入MS-SQL和MySQL表?

MS-SQL查詢有效地生成了一個索引,然後我想拉回所有與此查詢結果匹配的MySQL記錄。 (我可以帶回兩個表,即MySQL的未過濾數據,然後使用Linq進行過濾,但這樣會效率低下,因爲我將拉回比我需要更多的數據。)

MS-SQL查詢是通過LINQ的完成:

var fb1 = from f in db.tl_feedbacks 
      where f.timestamp >= new DateTime(fromYear, fromMonth, fromDay) 
      && f.timestamp <= new DateTime(toYear, toMonth, toDay) 
      select new {f.requestID, f.tl_feedback_score.score }; 

這將帶給我們一個表是這樣的:

RequestID | score 
----------------- 
12345  | 1 
12349  | 3 
12446  | 3 

從這個我想從下面的MySQL只返回記錄查詢在上表中有一個請求ID:

SELECT wo.WORKORDERID, 
     COALESCE(ti.FIRST_NAME,'Not Assigned') AS 'Technician', 
     COALESCE(cd.CATEGORYNAME, 'Not Assigned') AS Category, 
     COALESCE(scd.NAME, 'Not Assigned') AS Subcategory, 
     wof.UDF_CHAR1 "Office Location" 
FROM WorkOrder_Threaded wot 
INNER JOIN WorkOrder wo ON wot.WORKORDERID=wo.WORKORDERID 
LEFT JOIN SDUser sdu ON wo.REQUESTERID=sdu.USERID 
LEFT JOIN AaaUser aau ON sdu.USERID=aau.USER_ID 
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID 
LEFT JOIN SDUser td ON wos.OWNERID=td.USERID 
LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID 
LEFT JOIN CategoryDefinition cd ON wos.CATEGORYID=cd.CATEGORYID 
LEFT JOIN SubCategoryDefinition scd ON wos.SUBCATEGORYID=scd.SUBCATEGORYID 
LEFT JOIN WorkOrder_Fields wof ON wo.WORKORDERID=wof.WORKORDERID 

即我只是想拉回來的記錄12345,在這個例子中12349和12446。最終,我想要一個單一表,其中包含requestID,分數以及MySQL查詢中的列。但是,如果我能夠獲得「已過濾」的MySQL表格,我可以在之後加入這兩個表格。我只是不想將MySQL重新帶回「未過濾」,因爲表格會很大。

回答

3

有了正確的OLEDB數據庫驅動程序(我只使用PGSQL完成了這個功能,所以我不能真正提醒),您可以在MSSQL中創建一個Linked Server。這裏是a walkthroughhere's another

select * from openquery(LinkedServerDb,'select * from remotetable') 

,並加入:

可以使用OPENQUERY在MSSQL如下然後查詢它

select 
    * 
from 
    openquery(LinkedServerDb,'select * from remotetable') remoteTable 
    join localTable on remotetable.someid=localtable.otherid