<?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 Loop for SQL statement in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175099#M43676</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I've got a script of 15 sql statements. All the statements are for one customer only (see below).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SQL SELECT dim1, dim2, dim3 FROM Customer1 Table1&lt;/P&gt;&lt;P&gt;SQL SELECT dim1, dim2, dim3 FROM Customer1 Table2&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;SQL SELECT dim1, dim2, dim3 FROM Customer1 Table15&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to create a &lt;B&gt;loop for all Customers&lt;/B&gt; listed in another sql table (over 300 records) and add a dimension for Customer to the script (db name). Could anyone help with the code, please?&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Przemek&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 03 Sep 2009 21:07:28 GMT</pubDate>
    <dc:creator />
    <dc:date>2009-09-03T21:07:28Z</dc:date>
    <item>
      <title>Loop for SQL statement</title>
      <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175099#M43676</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;I've got a script of 15 sql statements. All the statements are for one customer only (see below).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SQL SELECT dim1, dim2, dim3 FROM Customer1 Table1&lt;/P&gt;&lt;P&gt;SQL SELECT dim1, dim2, dim3 FROM Customer1 Table2&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;SQL SELECT dim1, dim2, dim3 FROM Customer1 Table15&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to create a &lt;B&gt;loop for all Customers&lt;/B&gt; listed in another sql table (over 300 records) and add a dimension for Customer to the script (db name). Could anyone help with the code, please?&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Przemek&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 Sep 2009 21:07:28 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175099#M43676</guid>
      <dc:creator />
      <dc:date>2009-09-03T21:07:28Z</dc:date>
    </item>
    <item>
      <title>Loop for SQL statement</title>
      <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175100#M43677</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Przemec,&lt;BR /&gt;I assume the Customer names are the table names. If this is correct, here is what you can do.&lt;BR /&gt;First, you need the number of Customers. It can be defined this way:&lt;BR /&gt;LET NumberOfCustomers = NoOfRows('table'); // table is the list of your Customers&lt;BR /&gt;Now you can use the loop:&lt;BR /&gt;FOR i = 0 to $(NumberOfCustomers)-1&lt;BR /&gt; LET C= peek('Customer', $(i), table)&lt;BR /&gt; SQL SELECT ... FROM $(C);&lt;BR /&gt;NEXT&lt;BR /&gt;I didn't test exactly this script, so it may require some adjustments.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 Sep 2009 22:14:28 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175100#M43677</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-09-03T22:14:28Z</dc:date>
    </item>
    <item>
      <title>Loop for SQL statement</title>
      <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175101#M43678</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for the reply, Micheal.&lt;/P&gt;&lt;P&gt;I've tested the script and feel we're almost there. I get &lt;I&gt;Incorrect syntax near 'FROM'&lt;/I&gt; error so there must be just a small mistake. Do you have any idea what it could be?&lt;/P&gt;&lt;P&gt;BR,&lt;/P&gt;&lt;P&gt;Przemek&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Sep 2009 00:06:52 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175101#M43678</guid>
      <dc:creator />
      <dc:date>2009-09-04T00:06:52Z</dc:date>
    </item>
    <item>
      <title>Loop for SQL statement</title>
      <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175102#M43679</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Take a look at the log file, it can show what is going on.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Sep 2009 00:09:08 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175102#M43679</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-09-04T00:09:08Z</dc:date>
    </item>
    <item>
      <title>Loop for SQL statement</title>
      <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175103#M43680</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Michael,&lt;/P&gt;&lt;P&gt;first Customer goes ok but next ones don't get db.table after &lt;I&gt;FROM&lt;/I&gt; and end up with the log (script reduced to 3 loops):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-style: italic"&gt;0015 Table1:&lt;/P&gt;&lt;P style="font-style: italic"&gt;0016 SQL SELECT&lt;/P&gt;&lt;P style="font-style: italic"&gt;0017 FROM CDN_Configuration.CDN.Bases&lt;/P&gt;&lt;P&gt;&lt;I&gt;&lt;/I&gt;&lt;I&gt;1 fields found: Customers, 183 lines fetched&lt;/I&gt;&lt;/P&gt;&lt;P style="font-style: italic"&gt;0021 FOR i=0 TO 2&lt;/P&gt;&lt;P style="font-style: italic"&gt;0022 LET C = peek('Customers',0,Table1)&lt;/P&gt;&lt;P style="font-style: italic"&gt;0023 SQL SELECT Document FROM CDN_Customer1.CDN.VatNag&lt;/P&gt;&lt;P&gt;&lt;I&gt;&lt;/I&gt; &lt;I&gt;&lt;/I&gt;&lt;I&gt;1 fields found: Document, 1 lines fetched&lt;/I&gt;&lt;/P&gt;&lt;P style="font-style: italic"&gt;0024 NEXT&lt;/P&gt;&lt;P style="font-style: italic"&gt;0022 LET C = peek('Customers',1,Table1)&lt;/P&gt;&lt;P style="font-style: italic"&gt;0023 SQL SELECT Document FROM&lt;/P&gt;&lt;P&gt;&lt;I&gt;Error: SQL Error:[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near 'FROM'.&lt;/I&gt;&lt;/P&gt;&lt;P style="font-style: italic"&gt;SQL Scriptline:&lt;/P&gt;&lt;P style="font-style: italic"&gt;SQL State:37000&lt;/P&gt;&lt;P style="font-style: italic"&gt;0024 NEXT&lt;/P&gt;&lt;P style="font-style: italic"&gt;0022 LET C = peek('Customers',2,Table1)&lt;/P&gt;&lt;P style="font-style: italic"&gt;0023 SQL SELECT Document FROM&lt;/P&gt;&lt;P&gt;&lt;I&gt;Error: SQL Error:[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near 'FROM'.&lt;/I&gt;&lt;/P&gt;&lt;P style="font-style: italic"&gt;SQL Scriptline:&lt;/P&gt;&lt;P style="font-style: italic"&gt;SQL State:37000&lt;/P&gt;&lt;P style="font-style: italic"&gt;0024 NEXT&lt;/P&gt;&lt;P&gt;&lt;I&gt;Execution finished.&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The wholAny idea what might be the reason?&lt;/P&gt;&lt;P&gt;BR,&lt;BR /&gt;Przemek&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Sep 2009 04:22:41 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175103#M43680</guid>
      <dc:creator />
      <dc:date>2009-09-04T04:22:41Z</dc:date>
    </item>
    <item>
      <title>Loop for SQL statement</title>
      <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175104#M43681</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It helps, thanks.&lt;BR /&gt;Looks like the variable C wasn't defined in the 2nd and 3rd loops. My syntax for peek() function wasn't quite correct. I'm sure that the table name should be in single quotes as well: peek('Customers',$(i),&lt;B&gt;'Table1'&lt;/B&gt;)&lt;BR /&gt;(Without quotes, the table is not properly defined, and peek() takes Customer field from the last loaded table by default. When it was Table1 - it had this field, and you have one correct C. Next time it's a new table, no Customer field - no value for C.)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Sep 2009 08:26:11 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175104#M43681</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-09-04T08:26:11Z</dc:date>
    </item>
    <item>
      <title>Loop for SQL statement</title>
      <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175105#M43682</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Marvelous! It works perfectly &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Just one more thing - I need to load 15 sql tables for all customers. I would like to load Customers table once and only add &lt;B&gt;tablename&lt;/B&gt; at each sql statements i.e.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;LET NumberOfCustomers = NoOfRows('table');&lt;/P&gt;&lt;P&gt;FOR i = 0 to $(NumberOfCustomers)-1&lt;/P&gt;&lt;P&gt;LET C= peek('Customer', $(i), 'Table1')&lt;/P&gt;&lt;P&gt;SQL SELECT ... FROM $(C)&lt;STRONG&gt;+tablename&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;NEXT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I added it as above I got an error &lt;I&gt;Incorrect syntax near '+'. Do you know how to fix it?&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-style: italic"&gt;BR,&lt;/P&gt;&lt;P style="font-style: italic"&gt;Przemek&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 Sep 2009 22:43:59 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175105#M43682</guid>
      <dc:creator />
      <dc:date>2009-09-04T22:43:59Z</dc:date>
    </item>
    <item>
      <title>Loop for SQL statement</title>
      <link>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175106#M43683</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Getting there... &lt;IMG alt="Cool" src="http://community.qlik.com/emoticons/emotion-11.gif" /&gt;&lt;BR /&gt;You have to change the variable C itself:&lt;BR /&gt;...&lt;BR /&gt;LET C= peek('Customer', $(i), 'Table1') &lt;STRONG&gt;&amp;amp; tablename&lt;/STRONG&gt;&lt;BR /&gt;SQL SELECT ... FROM $(C);&lt;BR /&gt;...&lt;BR /&gt;If it doesn't work - see in the log file if the script creates the correct table name, and adjust if needed.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 Sep 2009 00:13:52 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Loop-for-SQL-statement/m-p/175106#M43683</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2009-09-05T00:13:52Z</dc:date>
    </item>
  </channel>
</rss>

