web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Convert Fetch XML to SQL Query

(1) ShareShare
ReportReport
Posted on by 12,119 Moderator

Hi all

Below is my Fetch XML

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="contact">
<attribute name="telephone1" />
<attribute name="contactid" />
<attribute name="emailaddress1" />
<attribute name="lastname" />
<attribute name="jobtitle" />
<attribute name="firstname" />
<attribute name="parentcustomerid" />
<order attribute="firstname" descending="false" />
<filter type="and">
<condition attribute="emailaddress1" operator="not-null" />
</filter>
<link-entity name="account" from="accountid" to="parentcustomerid" alias="bd">
<filter type="and">
<condition attribute="aw_segmentid" operator="in">
<value uiname="E" uitype="aw_segment">{12345678-1234-5678-1234-123456789123}</value>
<value uiname="S" uitype="aw_segment">{12345678-1234-5678-1234-123456789124}</value>
</condition>
</filter>
<link-entity name="opportunity" from="customerid" to="accountid" alias="be">
<filter type="and">
<condition attribute="aw_payment" operator="ne" uiname="V" uitype="aw_payment" value="{12345678-1234-5678-1234-123456789125}" />
</filter>
</link-entity>
</link-entity>
</entity>
</fetch>

and below is my converted SQL query

SELECT DISTINCT contact.telephone1
, contact.contactid
, contact.emailaddress1
, contact.lastname
, contact.jobtitle
, contact.firstname
, contact.ParentCustomerIdName
FROM contact
INNER JOIN account AS bd
ON contact.parentcustomerid = bd.accountid
INNER JOIN opportunity AS be
ON contact.accountid = be.customerid
WHERE
bd.aw_segmentid in ('{12345678-1234-5678-1234-123456789123}','{12345678-1234-5678-1234-123456789124}') AND Contact.EMailAddress1 is not null OR
be.aw_payment !='{12345678-1234-5678-1234-123456789125}'
ORDER BY contact.firstname

The results of both are not matching. There is some problem, I know. Please mention my mistake on this query.

Thank You

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Abdul Wahab,

    There is a tool to convert MSCRM FetchXML to SQL query. Please see the link below.

    fetchxml2sql.codeplex.com

    Also, refer the following community thread.

    community.dynamics.com/.../151662

    Hope this helps you.

  • Suggested answer
    Community Member Profile Picture
    on at

    Please try this SQL:

    SELECT DISTINCT contact.telephone1

    , contact.contactid

    , contact.emailaddress1

    , contact.lastname

    , contact.jobtitle

    , contact.firstname

    , contact.ParentCustomerIdName

    FROM contact

    INNER JOIN account AS bd

    ON contact.parentcustomerid = bd.accountid AND

    bd.aw_segmentid in ('{12345678-1234-5678-1234-123456789123}','{12345678-1234-5678-1234-123456789124}')

    INNER JOIN opportunity AS be

    ON contact.accountid = be.customerid AND

    be.aw_payment !='{12345678-1234-5678-1234-123456789125}'

    WHERE

    Contact.EMailAddress1 is not null

    ORDER BY contact.firstname

  • Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Nithya Gopinath

    I used this fetchxml2sql but same inaccurate results

    Thank You

  • Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Mohd Saad Akhtar

    I have 5000 + records in crm but when I run in sql it gives only 42.

    Where am I wrong ? Please do let me know.

    Thank YOu

  • Community Member Profile Picture
    on at

    deleted

  • Community Member Profile Picture
    on at

    Are you running it on correct SQL instance. To verify it run a simple sql query to find number of records on contacts.

  • Suggested answer
    Community Member Profile Picture
    on at

    Try this qury once more. If it is not correct then try to build query by using two tables first then join the third table:

    SELECT DISTINCT contact.telephone1

    , contact.contactid

    , contact.emailaddress1

    , contact.lastname

    , contact.jobtitle

    , contact.firstname

    , contact.ParentCustomerIdName

    FROM contact

    Left JOIN account AS bd

    ON contact.parentcustomerid = bd.accountid AND

    bd.aw_segmentid in ('{12345678-1234-5678-1234-123456789123}','{12345678-1234-5678-1234-123456789124}')

    Left JOIN opportunity AS be

    ON bd.accountid = be.customerid AND

    be.aw_payment !='{12345678-1234-5678-1234-123456789125}'

    WHERE

    Contact.EMailAddress1 is not null

    ORDER BY contact.firstname

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    Hi,

    How your are executing FetchXML and SQL queries?

    If you are doing advanced find for FetchXML, then might be result is different than SQL query.

    When you are executing advanced find, user security applied to result and result is filtered based on user permission.

    When Executing SQL query with Filtered views, there is no security applied on result.

    You can try adding filtered view in sql query and execute FetchXML and SQL query with same user permission.

    Hope this will help..

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    try this query

    SELECT  DISTINCT contact.telephone1

    , contact.contactid

    , contact.emailaddress1

    , contact.lastname

    , contact.jobtitle

    , contact.firstname

    , contact.parentcustomerid

    FROM contact INNER JOIN account AS bd ON

    contact.parentcustomerid = bd.accountid

    INNER JOIN opportunity AS be ON

    contact.accountid = be.customerid

    WHERE ((

    contact.emailaddress1 is not null)) AND (((

    bd.aw_segmentid in ({12345678-1234-5678-1234-123456789123},{12345678-1234-5678-1234-123456789124})))) AND (((

    be.aw_payment !='{12345678-1234-5678-1234-123456789125}')))

    ORDER BY contact.firstname  ASC

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans