Skip to main content
Dynamics 365 Community / Forums / Finance forum / How to use in clause t...
Finance forum
Suggested answer

How to use in clause to search multiple comma seperated values in x++

editSubscribe (1) ShareShare
ReportReport
Posted on by 97
Hi everyone i have a requirement where i want to search container values in select query
below is a demo example 
 
container test = [‘A’ , ‘B’];
DemoTable Demotable;// it has one field teststring of string type
 
String testStr = con2str(test);// comma seperated values of container
 
select demotable where demotable.teststring in test
 
the above query gives an error and reason behind that IN clause is only supported with enums like if i have container with enum values then the above query would have worked
 Query : is there any other way to search comma seperated values in select statement in d365 or search container values in sql query in x++
 
thanks for the help and replies  
  • Martin Dráb Profile Picture
    Martin Dráb 222,570 Super User on at
    How to use in clause to search multiple comma seperated values in x++
    ... but you would still need to iterate the container as shown in my code. You'd just first add values to a list instead of directly adding a range.
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 3,513 UG Leader on at
    How to use in clause to search multiple comma seperated values in x++
    Hi,
    i think you can use the List into the range value.
    e.g.
    Query                   query;
    QueryBuildDataSource    qbds;
    List                    listRange;
    queryBuildRange         qbr;
    
    listRange   = new List(Types::String);
    query       = new Query();
    qbds        = query.addDataSource(tableNum(<YourTable>));
    
    listRange.addEnd("1st value");
    listRange.addEnd("2nd value");
    .
    .
    listRange.addEnd("n value");
    qbr         = qbds.addRange(fieldNum(<YourTable>, <FieldRange>));
    qbr.value(SysQueryRangeUtil::queryListRange(rangeList));
    Best regards,
    Mohamed Amine Mahmoudi
     
  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,402 Super User on at
    How to use in clause to search multiple comma seperated values in x++
    Adding to Martin you can also do like,
     
    loop through container and inside the loop select the table buffer using conpeek().
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 222,570 Super User on at
    How to use in clause to search multiple comma seperated values in x++
    Use Query* classes instead of the select statement. Iterate the container and add a range for each value. For example:
    Query query = new Query();
    QueryBuildDataSource qbds = query.addDataSource(tableNum(DemoTable));
    
    for (int i = 1; i <= conLen(test); i++)
    {
        qbds.addRange(fieldNum(DemoTable, TestString)).value(queryValue(conPeek(test, i)));
    }
    
    QueryRun qr = new QueryRun(query);
    while (qr.next())
    {
        DemoTable demoTable = qr.getNo(1);
    }

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,045 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,570 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans