<?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>topic How do you select using optional parameters as filters? in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557020#M1430626</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am doing it using 2^n SELECT statements where n = number of optional parameters. I think I am doing it wrong. Please help me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-&lt;/P&gt;&lt;HR originaltext="------" /&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;To illustrate this&lt;/STRONG&gt;, I have two optional parameters: &lt;EM&gt;param1&lt;/EM&gt; and &lt;EM&gt;param2&lt;/EM&gt;. If the user does not input a value in either parameter, it will not be included in the select statement. So I have FOUR select statements to determine the existence of user input.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
IF param1 EQ "" AND param2 EQ "".

    SELECT * FROM transparent_table...
    " without filters since param1 and param2 are both blank

ELSEIF param1 NE "" AND param2 EQ "".

    SELECT * FROM transparent_table...
        WHERE field1 = param1
        " using only param1 as filter

ELSEIF param1 EQ "" AND param2 NE "".

    SELECT * FROM transparent_table...
        WHERE field2 = param2
        " using only param2 as filter

ELSEIF param1 NE "" AND param2 NE "".

    SELECT * FROM transparent_table...
        WHERE field1 = param1 AND field2 = param2
        " using both param1 and param2 as filters

ENDIF.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is still tolerable but the problem obviously appears as the number of optional parameters/select-options increases. Doing this kind of programming, I would have to make 8 SELECT statements for 3 optional parameters, and 16 SELECTs for 4 optional parameters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Is there a better way to do this?&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 27 Jan 2010 02:47:17 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2010-01-27T02:47:17Z</dc:date>
    <item>
      <title>How do you select using optional parameters as filters?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557020#M1430626</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am doing it using 2^n SELECT statements where n = number of optional parameters. I think I am doing it wrong. Please help me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-&lt;/P&gt;&lt;HR originaltext="------" /&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;To illustrate this&lt;/STRONG&gt;, I have two optional parameters: &lt;EM&gt;param1&lt;/EM&gt; and &lt;EM&gt;param2&lt;/EM&gt;. If the user does not input a value in either parameter, it will not be included in the select statement. So I have FOUR select statements to determine the existence of user input.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
IF param1 EQ "" AND param2 EQ "".

    SELECT * FROM transparent_table...
    " without filters since param1 and param2 are both blank

ELSEIF param1 NE "" AND param2 EQ "".

    SELECT * FROM transparent_table...
        WHERE field1 = param1
        " using only param1 as filter

ELSEIF param1 EQ "" AND param2 NE "".

    SELECT * FROM transparent_table...
        WHERE field2 = param2
        " using only param2 as filter

ELSEIF param1 NE "" AND param2 NE "".

    SELECT * FROM transparent_table...
        WHERE field1 = param1 AND field2 = param2
        " using both param1 and param2 as filters

ENDIF.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is still tolerable but the problem obviously appears as the number of optional parameters/select-options increases. Doing this kind of programming, I would have to make 8 SELECT statements for 3 optional parameters, and 16 SELECTs for 4 optional parameters.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Is there a better way to do this?&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jan 2010 02:47:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557020#M1430626</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-01-27T02:47:17Z</dc:date>
    </item>
    <item>
      <title>Re: How do you select using optional parameters as filters?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557021#M1430627</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I can think of 3 ways to achieve this - there are probably more... have a look at the demo code below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jonathan&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
report zsd_jc_dynamic_select.
tables:
  t001.
parameters:
  p_parm1               type t001-bukrs,
  p_parm2               type t001-waers.
select-options: "Option 3 - make Sel Opt look like a single param
  s_tip3                for t001-spras no-extension no intervals.
start-of-selection.
  perform demo_code.

form demo_code.
  data:
    l_where             type text72.
  ranges:
    lr_bukrs            for t001-bukrs,
    lr_waers            for t001-waers.
*
* Option 1 - range tables
  if not p_parm1 is initial.
    clear lr_bukrs.
    lr_bukrs-sign   = 'I'.
    lr_bukrs-option = 'EQ'.
    lr_bukrs-low    = p_parm1.
    append lr_bukrs.
  endif.

  if not p_parm2 is initial.
    clear lr_waers.
    lr_waers-sign   = 'I'.
    lr_waers-option = 'EQ'.
    lr_waers-low    = p_parm2.
    append lr_waers.
  endif.

  select *
    from t001
    where bukrs in lr_bukrs
    and   waers in lr_waers
    and   spras in s_tip3.
    write: / 'Company', t001-bukrs, 'Curr', t001-waers, 'Land', t001-land1.
  endselect.
*
* Option 2 - dynamic where
  uline.
  if not p_parm1 is initial.
    if not l_where is initial.
      concatenate l_where 'AND~' into l_where separated by space.
    endif.
    concatenate
      l_where
      'BUKRS =~' '''' p_parm1 ''''
      into l_where.
    replace 'AND~' with 'AND ' into l_where.
    replace '=~' with '= ' into l_where.
  endif.

  if not p_parm2 is initial.
    if not l_where is initial.
      concatenate l_where 'AND~' into l_where separated by space.
    endif.
    concatenate
      l_where
      'WAERS =~' '''' p_parm2 ''''
      into l_where.
    replace 'AND~' with 'AND ' into l_where.
    replace '=~' with '= ' into l_where.
  endif.

  select *
    from t001
    where (l_where)
    and   spras in s_tip3.
    write: / 'Company', t001-bukrs, 'Curr', t001-waers, 'Land', t001-land1.
  endselect.

endform. 
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jan 2010 03:41:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557021#M1430627</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-01-27T03:41:17Z</dc:date>
    </item>
    <item>
      <title>Re: How do you select using optional parameters as filters?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557022#M1430628</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Jonathan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Actually, after much thinking after already posting my question, I have already thought of transforming the parameters into select options (ranges). Neverthless, great ideas! I'll keep them in mind.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kyle&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jan 2010 05:19:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557022#M1430628</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-01-27T05:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: How do you select using optional parameters as filters?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557023#M1430629</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN __default_attr="18" __jive_macro_name="size"&gt;
&lt;STRONG&gt;I would prefer the orginal option !!!&lt;/STRONG&gt;
&lt;STRONG&gt;No range, no dynamic coding, just plain written OPEN SQL !!!&lt;/STRONG&gt;
&lt;STRONG&gt;Readability and correctness of the code will be much better.&lt;/STRONG&gt;
&lt;STRONG&gt;Performance is equal as the code which goes to the database is identical.&lt;/STRONG&gt; 
&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jan 2010 09:21:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557023#M1430629</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-01-27T09:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do you select using optional parameters as filters?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557024#M1430630</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry... Deleted be me, because the cuestion is really answered... &lt;SPAN __jive_emoticon_name="happy"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited by: Diego Alvarez on Jan 27, 2010 11:30 AM&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jan 2010 10:28:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/how-do-you-select-using-optional-parameters-as-filters/m-p/6557024#M1430630</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2010-01-27T10:28:31Z</dc:date>
    </item>
  </channel>
</rss>

