<?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: Hybrid static/dynamic SQL in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833489#M4864332</link>
    <description>&lt;P&gt;Good suggestion.  I just edited my question to add that tag.  I'll remember this going forward.&lt;/P&gt;</description>
    <pubDate>Fri, 09 Dec 2022 15:43:27 GMT</pubDate>
    <dc:creator>dhkom</dc:creator>
    <dc:date>2022-12-09T15:43:27Z</dc:date>
    <item>
      <title>Hybrid static/dynamic SQL</title>
      <link>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaq-p/13833484</link>
      <description>&lt;P&gt;I had such a good response to my post of a few days ago ('IN search condition - expression-list represented by a variable'), that I thought I'd try asking another question that had been on my mind but was not particularly urgent.  &lt;/P&gt;
&lt;P&gt;Following is a very simplified example of something I recently wished I could do:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;begin
  declare table_string char (100);
    --
    set table_string = 'mytable';
    --
    select * from dyneval(table_string);
end
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;In the above, 'dyneval(...)' is my invention for a function that would evaluate the string as 'dynamic sql' and include the resulting text in the static SQL of it's context.  Thus the select statement would resolve to:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;select * from mytable;
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;A good question is, why would I ever want to do that?  I had not come up with such a wish in my decades of writing SQL until recently.  The recent circumstance is as follows:&lt;/P&gt;
&lt;P&gt;We have a customer that has a number of instances of our database, each with the same schema.  We have created a separate 'multi-database reporting' database that has a remote server for each (CREATE SERVER statement).  The multi-db reporting database has a set of proxy tables pointing to the same tables on each remote server.  For example, we could have remote servers "server1" through "server8", and identically structured proxy tables &lt;CODE&gt;"remote_server1_tablea"&lt;/CODE&gt; through &lt;CODE&gt;"remote_server8_tablea"&lt;/CODE&gt;.  &lt;/P&gt;
&lt;P&gt;You probably can see where this is going.  We run the same query against each of the &lt;CODE&gt;"remote_server&amp;lt;n&amp;gt;_tablea"&lt;/CODE&gt; proxy tables (joining in other database-specific proxy tables), accumulating all the results in a single local table which then drives our report.&lt;/P&gt;
&lt;P&gt;Thus, assuming we have a local table "remote_server" with the name of each remote server, we could write something like the following to accumulate the data from all databases:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;begin
  for for_remote_table as cur_remote_table dynamic scroll cursor for
    select 
      server_name as s_server_name
    from
      remote_server
  do
    insert into accumlator_table (
      col1, col2, etc
    )
      select
        val1,
        val2,
        etc
      from
        dyneval ('remote_' || s_server_name || '_tablea')
    ;
  end for;
end
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;Right now we are repeatedly building a dynamic SQL string to add to the accumulator table, and doing "execute immediate".  This works but is somewhat awkward.  &lt;/P&gt;
&lt;P&gt;In my imagination, 'dyneval(...)' could be used to emit any part of a SQL statement - from clause, where clause, select list, etc.  I think it could really simplify use of dynamic SQL - i.e. the only dynamic SQL is the parts that have to be dynamic.&lt;/P&gt;
&lt;P&gt;I'm interested in thoughts on this, and particularly whether there is a way I am missing to accomplish my end using SQL Anywhere capabilities already in place (hope I'm not missing something simple).  &lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 09:43:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaq-p/13833484</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-09T09:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Hybrid static/dynamic SQL</title>
      <link>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833485#M4864328</link>
      <description>&lt;P&gt;Using v17, you can use &lt;STRONG&gt;indirect identifiers and/or TABLEREFs&lt;/STRONG&gt; to parametrize SQL statements w.r.t. table and columns names and the like, without having to use dynamic SQL.&lt;/P&gt;
&lt;P&gt;See here:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;P&gt;&lt;A href="https://help.sap.com/docs/SAP_SQL_Anywhere/93079d4ba8e44920ae63ffb4def91f5b/ce224c029e8c482c9fb637f5df3d43e4.html"&gt;Indirect identifiers&lt;/A&gt;&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;A href="https://help.sap.com/docs/SAP_SQL_Anywhere/93079d4ba8e44920ae63ffb4def91f5b/4ad519b5522b45b098bee702a965bbcd.html"&gt;TABLE REF data type&lt;/A&gt;&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;It's not as powerful as your fictional "dyneval()" function, if my understanding is correct, as it does not substitute complete clauses of a query - but you can parametrize identifiers within each part of a query, AFAIK, so I guess it should work fine.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 10:04:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833485#M4864328</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2022-12-09T10:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: Hybrid static/dynamic SQL</title>
      <link>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833487#M4864330</link>
      <description>&lt;P&gt;Fantastic!  Thanks, Volker.  This will definitely work for my need.  I must say I still like my fictional "dyneval()" function for dynamic where clauses in static SQL.  Maybe fiction will become reality some day?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 11:00:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833487#M4864330</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-09T11:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: Hybrid static/dynamic SQL</title>
      <link>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833488#M4864331</link>
      <description>&lt;P&gt;You can always suggest features in this forum and mark them with the "product-suggestion" tag, such as those:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://sqlanywhere-forum.sap.com/tags/product-suggestion/"&gt;https://sqlanywhere-forum.sap.com/tags/product-suggestion/&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;AFAIK, the SQL Anywhere engineers do read these postings. As just another customer, I don't know their priorities, obviously...&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 11:06:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833488#M4864331</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2022-12-09T11:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Hybrid static/dynamic SQL</title>
      <link>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833489#M4864332</link>
      <description>&lt;P&gt;Good suggestion.  I just edited my question to add that tag.  I'll remember this going forward.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2022 15:43:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833489#M4864332</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-09T15:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Hybrid static/dynamic SQL</title>
      <link>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833486#M4864329</link>
      <description>&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;begin
  declare table_string varchar(100);
    --
    set table_string = 'mytable';
    --
    select * from `[table_string]`;
end
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;Be sure to use the correct quotes!!!&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2022 04:01:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833486#M4864329</guid>
      <dc:creator>awitter43</dc:creator>
      <dc:date>2022-12-12T04:01:31Z</dc:date>
    </item>
    <item>
      <title>Re: Hybrid static/dynamic SQL</title>
      <link>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833490#M4864333</link>
      <description>&lt;P&gt;Thanks.  This is, the "Indirect identifier" of the first answer - a feature new to SA17 that I was not aware of (but am now).  I continue to think my suggestion is more flexible and powerful, but of course, am not aware of the possible complexities/complications.  Perhaps it will be considered.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2022 08:34:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/hybrid-static-dynamic-sql/qaa-p/13833490#M4864333</guid>
      <dc:creator>dhkom</dc:creator>
      <dc:date>2022-12-12T08:34:06Z</dc:date>
    </item>
  </channel>
</rss>

