<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: IN search condition - expression-list represented by a variable in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833569#M4864412</link>
    <description>&lt;P&gt;Thank you.  I was also not aware of openstring(...).  It looks like you are using openstring(...) to the same end as sa_split_list(...) in the prior answer.  I would say sa_split_list(...) is a bit simpler, but this definitely works.&lt;/P&gt;</description>
    <pubDate>Thu, 08 Dec 2022 09:01:33 GMT</pubDate>
    <dc:creator>dhkom</dc:creator>
    <dc:date>2022-12-08T09:01:33Z</dc:date>
    <item>
      <title>IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaq-p/13833557</link>
      <description>&lt;P&gt;Can anyone suggest a clever way to do something like this without resorting to dynamic SQL?  Please excuse me if I'm just missing something simple.&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;begin
declare s_id_lst        char (30);
--
set s_id_lst = '3,4';
--
select
    list(foo_nm)
from
    my_table
where
    foo_id in (s_id_lst)
;
end;
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 07 Dec 2022 11:39:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaq-p/13833557</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-07T11:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833558#M4864401</link>
      <description>&lt;P&gt;Here's an approach using an ARRAY to hold a variable number of IN list elements.
Instead of building an IN clause and using it within a WHERE clause, the UNNEST operator is used to generate a derived table from the array, which then is joined with the real table. (And here sa_rowgenerator() is used to mimic a real table.&lt;/P&gt;
&lt;PRE&gt;begin
   -- use an array for the desired IN-list values
   declare s_id_arr array of int;
   set s_id_arr = array(3, 4, 5, 8, 10, 98);

   select list(foo_num order by foo_num)
   from
   -- dummy table via sa_rowgenerator()
     (select row_num as foo_num from sa_rowgenerator(1, 100)) SP
      inner join unnest(s_id_arr) as UN(ID) on SP.foo_num = UN.ID;
end;
&lt;/PRE&gt;

&lt;P&gt;returns 3,4,5,8,10,98&lt;/P&gt;
&lt;P&gt;Now the next question is: How can you set the array contents outside this SQL block?&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;At least within DBISQL, it's not that difficult, say, via a stored procedure:&lt;/P&gt;
&lt;PRE&gt;create or replace procedure SP_FilterViaArray(s_id_arr array of int)
result (list_of_ids long varchar)
begin
   select list(foo_num order by foo_num)
   from
   -- dummy table via sa_rowgenerator()
      (select row_num as foo_num from sa_rowgenerator(1, 100)) SP
         inner join unnest(s_id_arr) as UN(ID) on SP.foo_num = UN.ID;
end;

-- Use an ARRAY constructor as argument
call SP_FilterViaArray(array(9, 8, 7));
select * from SP_FilterViaArray(array(1, 2, 3));
select * from SP_FilterViaArray(array('3', '4', '5', '8', '100', 101));
-- or even build the argument dynamically via another sa_rowgenerator() call
call SP_FilterViaArray(array(select row_num from sa_rowgenerator(10, 21, 3)));
&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Dec 2022 12:53:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833558#M4864401</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2022-12-07T12:53:46Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833563#M4864406</link>
      <description>&lt;P&gt;Wow - Thanks, Volker.  At least I don't have to feel sheepish: Simple it wasn't!  I've not used arrays before in SQL Anywhere, and had not come across unnest(...) and sa_rowgenerator(...) either.  New capabilities for me to consider.  &lt;/P&gt;
&lt;P&gt;One potential limitation of this solution is you have to put an upper limit on the 'id' values - 100, in your example code.  Indeed your last usage example of SP_FilterViaArray(...) does not return '101' because it is beyond the assumed limit.  &lt;/P&gt;
&lt;P&gt;Do you have an opinion on the performance of this solution versus just going ahead with a simpler dynamic sql select?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 13:32:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833563#M4864406</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-07T13:32:38Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833564#M4864407</link>
      <description>&lt;P&gt;The upper limit 100 here is just an effect of the "base" table, which I constructed to contain the numbers from 1 to 100, so that's just an limit of the sample. If you use your original table instead, apparently it would only be limited by the datatype of its column and/or the array base type. The array itself is limited to 6.4 million elements according to the docs...&lt;/P&gt;
&lt;P&gt;I can't tell on the actual performance but turning lists into derived tables and joining those is something we do use a lot and have never felt negative performance implications. In contrast, I think joins usually are faster than tests via long IN lists, and the SQL Anywhere query engine might do comparable steps during query optimization.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 13:42:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833564#M4864407</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2022-12-07T13:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833565#M4864408</link>
      <description>&lt;P&gt;Certainly very long IN lists seem to be bad news. It's a very cunning approach Volker!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 16:18:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833565#M4864408</guid>
      <dc:creator>justin_willey</dc:creator>
      <dc:date>2022-12-07T16:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833566#M4864409</link>
      <description>&lt;P&gt;Thanks again.  Cunning indeed!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 17:02:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833566#M4864409</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-07T17:02:20Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833559#M4864402</link>
      <description>&lt;P&gt;Another way to achieve this is by using &lt;STRONG&gt;execute immediate&lt;/STRONG&gt; 
&lt;/P&gt;&lt;PRE&gt;begin
declare s_id_lst        char (30);
--
set s_id_lst = '3,4';
--
execute immediate 'select '||
                  'list(foo_nm) '||
                  'from '||
                  'my_table '||
                  'where '||
                  'foo_id in ('|| s_id_lst ||')';
end;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;You could also create a long varchar variable for constructing the sql statement and then use that variable to execute the statement
&lt;/P&gt;&lt;PRE&gt;execute immediate variable_Statement;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 02:31:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833559#M4864402</guid>
      <dc:creator>fvestjens</dc:creator>
      <dc:date>2022-12-08T02:31:03Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833560#M4864403</link>
      <description>&lt;P&gt;In addition to my ARRAY approach, here's a "less cunning" version with a simple comma-separated list of values as a string, using the builtin sa_split_list() procedure to turn that into a derived table. This should work with SQL Anywhere 10 and above:&lt;/P&gt;
&lt;PRE&gt;create or replace procedure SP_FilterViaList(s_id_list long varchar)
result (list_of_ids long varchar)
begin
   select list(foo_num order by foo_num)
   from
   -- dummy table via sa_rowgenerator()
      (select row_num as foo_num from sa_rowgenerator(1, 100)) MyDummyTable
         inner join sa_split_list(s_id_list, ',') SP on MyDummyTable.foo_num = SP.row_value;
end;

-- Use a comma-separated list of values
call SP_FilterViaList('9, 8, 7');
select * from SP_FilterViaList('1, 2, 3');&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Dec 2022 02:56:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833560#M4864403</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2022-12-08T02:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833561#M4864404</link>
      <description>&lt;P&gt;How about creating a procedure with the arguments as comma separated long nvarchar and use that in your select statement:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;create or replace procedure List2Table(
  in ValueList long nvarchar
)
begin
  select item 
  from 
    openstring(value ValueList) 
      with("item" nchar(40)) 
      option (delimited by ';' row delimited by ',')
    as "items"
end;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;Then use this procedure in your query:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;select
  list(foo_nm)
from
  my_table
where
  foo_id in (select item from List2Table(s_id_lst))
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 08 Dec 2022 03:17:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833561#M4864404</guid>
      <dc:creator>awitter43</dc:creator>
      <dc:date>2022-12-08T03:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833562#M4864405</link>
      <description>&lt;P&gt;My method is a bit different but I use it a lot, often building lists from user queries/input.
Works well and allows joins on multiple fields rather than just 1.
The index speeds things up if the input list is big.&lt;/P&gt;
&lt;P&gt;declare local temporary table "zt_tmp"(
  "myid" integer not null default-1,
 ) not transactional;&lt;/P&gt;
&lt;P&gt;create index if not exists zt_tmp_id on zt_tmp(myid asc);&lt;/P&gt;
&lt;P&gt;-- insert your values into the tmp table&lt;/P&gt;
&lt;P&gt;select *
from my_table as MT
join "zt_tmp" as Z on MT.foo_id = Z.myid;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 06:14:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833562#M4864405</guid>
      <dc:creator>timcheshire</dc:creator>
      <dc:date>2022-12-08T06:14:14Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833567#M4864410</link>
      <description>&lt;P&gt;Thank you, but my question did say, "without resorting to dynamic SQL".&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 08:29:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833567#M4864410</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-08T08:29:28Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833568#M4864411</link>
      <description>&lt;P&gt;Thanks, Volker.  I really like this one - it is much simpler.  I was also not aware of sa_split_list(...).  For the record, the following is a working solution to my original question example - and it does not require sa_rowgenerator() with an estimate of a maximum number of rows to generate:&lt;/P&gt;
&lt;P&gt;&lt;CODE&gt;
begin
  declare s_id_lst        char (30);
  --
  set s_id_lst = '3,4';
  --
  select
      list(mt.foo_nm)
  from
      sa_split_list(s_id_lst, ',') sp join my_table mt
        on (sp.row_value = mt.foo_id)
  ;
end;
&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 08:54:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833568#M4864411</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-08T08:54:04Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833569#M4864412</link>
      <description>&lt;P&gt;Thank you.  I was also not aware of openstring(...).  It looks like you are using openstring(...) to the same end as sa_split_list(...) in the prior answer.  I would say sa_split_list(...) is a bit simpler, but this definitely works.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 09:01:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833569#M4864412</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-08T09:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833570#M4864413</link>
      <description>&lt;P&gt;Ok, missed that.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 09:12:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833570#M4864413</guid>
      <dc:creator>fvestjens</dc:creator>
      <dc:date>2022-12-08T09:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833571#M4864414</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;and it does not require sa_rowgenerator() with an estimate of a maximum number of rows to generate&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It's a misunderstanding – I just used sa_rowgenerator to generate a dummy table so the variable list of values could be tested against something. I could just as well had a regular table with some values in my sample, and of course, you would use &lt;EM&gt;your&lt;/EM&gt; sample table.&lt;/P&gt;
&lt;P&gt;That being said, I also think this is easier than the ARRAY approach. I very freqently use sa_split_list() but rarely use arrays, so the ARRAY approach was more fun on my part... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 09:33:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833571#M4864414</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2022-12-08T09:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833572#M4864415</link>
      <description>&lt;P&gt;No worries.  I've done similar!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2022 16:29:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833572#M4864415</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-08T16:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: IN search condition - expression-list represented by a variable</title>
      <link>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833573#M4864416</link>
      <description>&lt;P&gt;FWIW, I like those kind of questions in this forum and the various solutions... usually offering something to learn for everyone &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 04:30:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/in-search-condition-expression-list-represented-by-a-variable/qaa-p/13833573#M4864416</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2022-12-09T04:30:27Z</dc:date>
    </item>
  </channel>
</rss>

