我想在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查询中那些在上表中有RequestID的记录:
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带回“未经过滤”,因为表格会很大。
答案 0 :(得分:3)
使用正确的OLEDB数据库驱动程序(我只用PGSQL完成此操作,所以我无法提出建议),您可以在MSSQL中创建Linked Server。这是a walkthrough和here's another。
然后,您可以使用OPENQUERY在MSSQL中查询它:
select * from openquery(LinkedServerDb,'select * from remotetable')
并加入:
select
*
from
openquery(LinkedServerDb,'select * from remotetable') remoteTable
join localTable on remotetable.someid=localtable.otherid