【问题标题】:Left outer joins in FetchXML with multiple conditions具有多个条件的 FetchXML 中的左外连接
【发布时间】:2018-08-16 06:03:46
【问题描述】:

我正在尝试在 FetchXML 中使用多个条件进行左外连接。

这是我想要实现的大致 SQL:

SELECT incident.*, externalCheck.*
FROM incident
LEFT OUTER JOIN externalCheck 
  ON externalCheck.incidentId = incident.incidentId
  AND externalCheck.checkType = 1
  AND externalCheck.isLatest = 1;

注意:这应该始终返回 1:1 的关系,因为我们的业务逻辑要求每个 checkType 只有一个 isLatest

这是我的 FetchXML:

<entity name="incident">
  <all-attributes />
  <link-entity name="externalCheck" from="incidentId" to="incidentId" link-type="outer">
    <filter type="and">
      <condition attribute="checkType" operator="eq" value="1" />
      <condition attribute="isLatest" operator="eq" value="1" />
    </filter>
    <all-attributes />
  </link-entity>
</entity>

问题

问题是连接右侧为空的事件记录(即没有 externalCheck 记录)没有被返回,而在左外连接中,我希望即使连接的右侧为空,仍会返回事件记录。

我怀疑 FetchXML 将我的过滤器转换为 WHERE 子句,而不是将条件添加到 ON 子句。

问题

谁能确认发生了什么,以及可能的解决方案?

【问题讨论】:

    标签: sql dynamics-crm fetchxml


    【解决方案1】:

    你的怀疑是正确的。但是你可以克服它。

    Fetchxml 很灵活,下面的 sn-p 将给出带有多个子句的左外连接的结果。

    <entity name="incident">
      <all-attributes />
      <link-entity name="externalCheck" alias="ext" from="incidentId" to="incidentId" link-type="outer">
        <filter type="and">
          <condition attribute="checkType" operator="eq" value="1" />
          <condition attribute="isLatest" operator="eq" value="1" />
        </filter>
      </link-entity>
       <filter>
        <condition entityname="ext" attribute="externalCheckid" operator= "null" />
       </filter>
    </entity>
    

    真正的问题是externalCheck.*,你无法获取相关的实体属性。必须从 link-entity 节点中删除 &lt;all-attributes /&gt;

    Read more

    【讨论】:

    • 嗨阿伦。感谢您的回答。我尝试了您的 XML 的各种版本,但我不断收到错误消息,即“实体名称”不是该条件的有效属性。没有实体名称,提取将运行,但它对返回的行数没有影响。
    • 看起来它应该可以工作,但 XrmToolBox 抱怨该属性。“entityname 属性不是链接实体的有效属性。”尽管我已经在条件节点上添加了属性,但这仍然存在。很奇怪。
    • @MarkMicallef 忘记了 xrmtoolbox,它是否在您的代码中执行并获取结果?
    猜你喜欢
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多