需要帮助sql查询来获取CRM 2011中的XML查询?

时间:2014-05-05 15:02:54

标签: xml dynamics-crm-2011 crm fetch fetchxml

我有这个查询

 select d.ope_supervisorName,d.ope_categoriaName ,d.ope_categoria,d.ope_cuota,d.ope_cuotaventadetId  
 ,f.Ope_name,c.ope_name
 from ope_cuotaventadet d
 join ope_figura f on d.Ope_supervisor=f.ope_figuraid
 join ope_categoriaproducto c on d.ope_categoria=c.ope_categoriaproductoid
 join ope_cuotaventa cv on d.ope_cuotaventaid =cv.ope_cuotaventaId 
 where cv.ope_cuotaventaId ='BEC262FA-23D2-E311-A801-00505697722F'
 union
 select f.Ope_name,c.ope_name,c.ope_categoriaproductoId ,cuota=0,id=newid(),
 f.Ope_name,c.ope_name 
  from ope_figura f
cross join ope_categoriaproducto c 
 where convert(varchar(50),c.ope_categoriaproductoId)+CONVERT(varchar(50),f.Ope_figuraId )
 not in (select convert(varchar(50),d.ope_categoria)+CONVERT(varchar(50), d.ope_supervisor ) from ope_cuotaventa cv 
 join ope_cuotaventadet d
 on d.ope_cuotaventaid =cv.ope_cuotaventaId
 where cv.ope_cuotaventaId ='BEC262FA-23D2-E311-A801-00505697722F'

  )
 and f.Ope_cedisid ='9D641606-EE5B-E011-AF04-00505697471A'

我读过fetch不支持交叉连接然后可以使用吗?

 join ope_categoriaproducto c on 1=1

然后我

select d.ope_supervisorName,d.ope_categoriaName ,d.ope_categoria,d.ope_cuota,d.ope_cuotaventadetId  
 ,f.Ope_name,c.ope_name
 from ope_cuotaventadet d
 join ope_figura f on d.Ope_supervisor=f.ope_figuraid
 join ope_categoriaproducto c on d.ope_categoria=c.ope_categoriaproductoid
 join ope_cuotaventa cv on d.ope_cuotaventaid =cv.ope_cuotaventaId 
 where cv.ope_cuotaventaId ='BEC262FA-23D2-E311-A801-00505697722F'
 union
 select f.Ope_name,c.ope_name,c.ope_categoriaproductoId ,cuota=0,id=newid(),
 f.Ope_name,c.ope_name 
  from ope_figura f
 join ope_categoriaproducto c on 1=1
 where convert(varchar(50),c.ope_categoriaproductoId)+CONVERT(varchar(50),f.Ope_figuraId )
 not in (select convert(varchar(50),d.ope_categoria)+CONVERT(varchar(50), d.ope_supervisor ) from ope_cuotaventa cv 
 join ope_cuotaventadet d
 on d.ope_cuotaventaid =cv.ope_cuotaventaId
 where cv.ope_cuotaventaId ='BEC262FA-23D2-E311-A801-00505697722F'

  )
 and f.Ope_cedisid ='9D641606-EE5B-E011-AF04-00505697471A'

到目前为止我有这个提取,我不知道如何添加联盟部分以及如何使用子查询进行处理

function querydet(Id) {
    var fetchXml = "";
    fetchXml += "   <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXml += "       <entity name='ope_cuotaventadet'>";
    fetchXml += "           <attribute name='ope_supervisorName'/>";
    fetchXml += "           <attribute name='ope_categoriaName'/>";
    fetchXml += "           <attribute name='ope_categoria'/>";
    fetchXml += "           <attribute name='ope_supervisor'/>";
    fetchXml += "           <attribute name='ope_cuota'/>";
    fetchXml += "           <attribute name='ope_cuotaventadetId'/>";
    fetchXml += "           <link-entity name='ope_cuotaventa' from='ope_cuotaventaId' to='ope_cuotaventaId' visible='false' link-type='outer'>";
    fetchXml += "               <attribute name='ope_cuotaventaId' />";
    fetchXml += "           <filter type='and'>";
    fetchXml += "               <condition attribute='ope_cuotaventaId' operator='eq' value='" + Id+ "'/>";
    fetchXml += "           </filter>";
    fetchXml += "           </link-entity>";
    fetchXml += "           <link-entity name='ope_figura' from='ope_figuraid' to='ope_supervisor' visible='false' link-type='outer'>";
    fetchXml += "               <attribute name='ope_name' />";
    fetchXml += "               <order attribute='ope_name' descending='false' />";
    fetchXml += "           </link-entity>";
    fetchXml += "           <link-entity name='ope_categoriaproducto' from='ope_categoriaproductoId' to='ope_categoria' visible='false' link-type='outer'>";
    fetchXml += "               <attribute name='ope_name' />";
    fetchXml += "               <order attribute='ope_name' descending='false' />";
    fetchXml += "           </link-entity>";
    fetchXml += "       </entity>";
    fetchXml += "   </fetch>";


    attribute = new Array();

    attribute[0] = new Object();
    attribute[0].ID = "ope_figuraid";
    attribute[0].Name = "ope_figuraid.ope_name";

    attribute[1] = new Object();
    attribute[1].ID = "ope_categoriaproductoid";
    attribute[1].Name = "ope_categoriaproductoid.ope_name";

    attribute[2] = new Object();
    attribute[2].Name = "ope_cuota";


 //    attribute[0].ID = "ope_inventableid";
 //    attribute[0].Name = "ope_inventableid.ope_name";

 //    attribute[1] = new Object();
 //    attribute[1].Name = "ope_codope";

 //    attribute[2] = new Object();
 //    attribute[2].Name = "ope_cantidad";

 //    attribute[3] = new Object();
 //    attribute[3].ID = "ope_ribe_prodcedisdetid";

 //    attribute[4] = new Object();
 //    attribute[4].Name = "ope_unidadid.ope_name"

    result = new Array();
    result = fetchWS(fetchXml, attribute);
    return result;
}

1 个答案:

答案 0 :(得分:1)

我一直使用fetchxml,但首先我写了QueryExpression,这很容易写,然后

转换QueryExpressiontoFetchXml。

https://community.dynamics.com/crm/b/mileyja/archive/2011/07/07/convert-queryexpression-to-fetchxml-using-net-or-jscript-with-the-microsoft-dynamics-crm-2011-sdk.aspx

您也可以使用Online Sql来获取FetchXml转换器:

http://www.sql2fetchxml.com/