<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[My Visual Database — sql in code question]]></title>
		<link>https://myvisualdatabase.com/forum/viewtopic.php?id=3122</link>
		<atom:link href="https://myvisualdatabase.com/forum/extern.php?action=feed&amp;tid=3122&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in sql in code question.]]></description>
		<lastBuildDate>Tue, 21 Feb 2017 20:23:28 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=19112#p19112</link>
			<description><![CDATA[<p>wow - thanks!</p>]]></description>
			<author><![CDATA[null@example.com (timlitw)]]></author>
			<pubDate>Tue, 21 Feb 2017 20:23:28 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=19112#p19112</guid>
		</item>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=19096#p19096</link>
			<description><![CDATA[<p>Hello timlitw,</p><br /><p>You are right, there is a way of getting multiple results from just one query, but this is not an array, this is a dataset.</p><br /><p>IF and only IF you use the SAME query multiple times to get multiples results, then you can simplify like :</p><br /><div class="codebox"><pre><code>procedure .....
var
     MyResults : TDataSet;
     x : Integer;
     y, z : String;
begin
     SQLQuery(&#039;SELECT x, y, z FROM my_table WHERE my_conditions&#039;,MyResults);
     while not MyResults.Eof do
          begin
               x := MyResults.FieldByName(&#039;x&#039;).AsInteger;
               y := MyResults.FieldByName(&#039;y&#039;).AsString;
               z := MyResults.FieldByName(&#039;z&#039;).AsString;

               ..... your awesome code here to use the x, y and z variables you just got in one single query

               MyResults.Next;
          end;
     MyResults.Free;

end;</code></pre></div><p>Don&#039;t forget the MyResults.Next or your application will hang and you&#039;ll get an error, and don&#039;t forget to Free the dataset at the end of the procedure or you might encounter memory leaks.</p><br /><p>You can use this to get multiples results with one query but also multiple sets of multiple results if you , for example, specify a range in your conditions. The &quot;while not MyResults.Eof&quot; means as long as there is data, loop through them, so you can have multiple loops with just one query.</p><p>Test this by putting a memo on a form and writing a query that fetches all your company names (for example), and output these names to the memo in the loop. If you have 10 companies, you&#039;ll get 10 lines with 10 names in your memo. With just one query <img src="https://myvisualdatabase.com/forum/img/smilies/smile.png" width="15" height="15" alt="smile" /></p><br /><p>have fun</p><br /><p>Cheers</p><br /><p>Mathias</p>]]></description>
			<author><![CDATA[null@example.com (mathmathou)]]></author>
			<pubDate>Tue, 21 Feb 2017 07:16:27 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=19096#p19096</guid>
		</item>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=19090#p19090</link>
			<description><![CDATA[<p>It has changed a bit but is working - I found out the price was by the product category so I had to add categories to the products and fill the category field by the product selected. but can I make this more efficient by running the SQL query only once to get both values?&nbsp; If so does it come back as an array? I guess I need to study arrays</p><div class="codebox"><pre><code>    flatrate := SQLExecute(&#039;SELECT FlatRate FROM FreightRateChart &#039;+
        &#039;LEFT OUTER JOIN TruckCompanies ON FreightRateChart.id_TruckCompanies = TruckCompanies.id &#039;+
        &#039;LEFT OUTER JOIN ProductsType ON FreightRateChart.id_ProductsType = ProductsType.id &#039;+
        &#039;LEFT OUTER JOIN LoadOrigin ON FreightRateChart.id_LoadOrigin = LoadOrigin.id &#039;+
        &#039;LEFT OUTER JOIN LoadDestination ON FreightRateChart.id_LoadDestination = LoadDestination.id &#039;+
        &#039;WHERE (TruckCompanies.Name = &#039;&#039;&#039;+CalcShipping.truckCombo.Text+&#039;&#039;&#039;) &#039;+
        &#039;AND (ProductsType.Name=&#039;&#039;&#039;+CalcShipping.prodtypecombo.Text+&#039;&#039;&#039;) &#039;+
        &#039;AND (LoadOrigin.Name=&#039;&#039;&#039;+CalcShipping.originCombo.Text+&#039;&#039;&#039;) &#039;+
        &#039;AND (LoadDestination.Name=&#039;&#039;&#039;+CalcShipping.destinationCombo.Text+&#039;&#039;&#039;) &#039;+
        &#039;ORDER BY FreightRateChart.id DESC LIMIT 1;&#039;);
    surcharge := SQLExecute(&#039;SELECT Surcharge FROM FreightRateChart &#039;+
        &#039;LEFT OUTER JOIN TruckCompanies ON FreightRateChart.id_TruckCompanies = TruckCompanies.id &#039;+
        &#039;LEFT OUTER JOIN ProductsType ON FreightRateChart.id_ProductsType = ProductsType.id &#039;+
        &#039;LEFT OUTER JOIN LoadOrigin ON FreightRateChart.id_LoadOrigin = LoadOrigin.id &#039;+
        &#039;LEFT OUTER JOIN LoadDestination ON FreightRateChart.id_LoadDestination = LoadDestination.id &#039;+
        &#039;WHERE (TruckCompanies.Name = &#039;&#039;&#039;+CalcShipping.truckCombo.Text+&#039;&#039;&#039;) &#039;+
        &#039;AND (ProductsType.Name=&#039;&#039;&#039;+CalcShipping.prodtypecombo.Text+&#039;&#039;&#039;) &#039;+
        &#039;AND (LoadOrigin.Name=&#039;&#039;&#039;+CalcShipping.originCombo.Text+&#039;&#039;&#039;) &#039;+
        &#039;AND (LoadDestination.Name=&#039;&#039;&#039;+CalcShipping.destinationCombo.Text+&#039;&#039;&#039;) &#039;+
        &#039;ORDER BY FreightRateChart.id DESC LIMIT 1;&#039;);

    CalcShipping.PriceTon.Text := flatrate;
    CalcShipping.SurchargePercent.Text := surcharge;</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (timlitw)]]></author>
			<pubDate>Mon, 20 Feb 2017 18:49:09 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=19090#p19090</guid>
		</item>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=19089#p19089</link>
			<description><![CDATA[<p>Sorry -&nbsp; I was away from project for a few days.&nbsp; Thaks for your help!</p>]]></description>
			<author><![CDATA[null@example.com (timlitw)]]></author>
			<pubDate>Mon, 20 Feb 2017 18:36:44 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=19089#p19089</guid>
		</item>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=18968#p18968</link>
			<description><![CDATA[<p>Hello again</p><br /><p>OK, I&#039;ve just checked&nbsp; and this works :</p><div class="codebox"><pre><code>flatrate := SQLExecute(&#039;SELECT FlatRate FROM FreightRateChart &#039;+
                            &#039;LEFT OUTER JOIN TruckCompanies ON FreightRateChart.id_TruckCompanies = TruckCompanies.id &#039;+
                            &#039;LEFT OUTER JOIN Products ON FreightRateChart.id_Products = Products.id &#039;+
                            &#039;LEFT OUTER JOIN LoadOrigin ON FreightRateChart.id_LoadOrigin = LoadOrigin.id &#039;+
                            &#039;LEFT OUTER JOIN LoadDestination ON FreightRateChart.id_LoadDestination = LoadDestination.id &#039;+
                            &#039;WHERE TruckCompanies.Name LIKE &#039;&#039;&#039; +Form1.truckCombo.Text+ &#039;&#039;&#039; &#039;+
                            &#039;AND Products.Name LIKE &#039;&#039;&#039; +Form1.productCombo.Text+ &#039;&#039;&#039; &#039;+
                            &#039;AND LoadOrigin.Name LIKE &#039;&#039;&#039; +Form1.originCombo.Text+ &#039;&#039;&#039; &#039;+
                            &#039;AND LoadDestination.Name LIKE &#039;&#039;&#039; +Form1.destinationCombo.Text+ &#039;&#039;&#039; &#039;+
                            &#039;ORDER BY FreightRateChart.id DESC LIMIT 1; &#039;);</code></pre></div><p>I tested it with &quot;LIKE &quot; and &quot;=&quot; and both works. Since you don&#039;t use the % in your query, you can use = instead of LIKE.</p><p>Also, the concatenation sign + helps making your query easier to read on multiple lines like in sqlstudio.</p><p>I added a </p><div class="codebox"><pre><code>     ShowMessage(flatrate);</code></pre></div><p>just to make sure there was a value returned.</p><p>The result I get with value :<br />- Trucking Co4<br />- Gas<br />- Origin1<br />- Dest1</p><p>is 46,72.</p><br /><p>Hope this helps</p><br /><p>Cheers</p><br /><p>Mathias</p><br /><p>PS : using v 3.2</p>]]></description>
			<author><![CDATA[null@example.com (mathmathou)]]></author>
			<pubDate>Tue, 14 Feb 2017 20:43:27 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=18968#p18968</guid>
		</item>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=18967#p18967</link>
			<description><![CDATA[<p>Hello timlitw,</p><br /><p>Just a wild guess, it&#039;s early in the morning and I&#039;m having my first coffee <img src="https://myvisualdatabase.com/forum/img/smilies/smile.png" width="15" height="15" alt="smile" /></p><br /><p>I don&#039;t see the logical operators in your second query. Seems you forgot the &quot;LIKE&quot; in the statement.</p><br /><p>The ORDER BY / LIMIT command is ok if you assume that the latest value saved in database is the highest. But what if this is not the case ?<br />You could also try something different with the statement &quot;HAVING MAX(X)&quot; if you are dealing with a integer (an ID for example or a number) but you&#039;ll have to had a &quot;GROUP BY&quot; command to use it.</p><br /><p>If you are dealing with other variables that numbers, a number stored as text for example, you can still use this value for sorting with the command CAST. This instruction will transtype (convert) a value from a type to another and help you sorting.<br />For example, if a number is stored as text, you can convert it and then use it for sorting with :<br />ORDER BY CAST(X AS INTEGER) ASC</p><br /><p>Finally, is you query included in the script or embedded in a SQL Query button ? Because the way you pass on the variables will be different in both cases.</p><br /><p>I&#039;ll have a look at your project when I reach my office. I know nothing better than a bit of code with another coffee to start a good day of work <img src="https://myvisualdatabase.com/forum/img/smilies/smile.png" width="15" height="15" alt="smile" /></p><br /><p>Cheers</p><br /><p>Math</p>]]></description>
			<author><![CDATA[null@example.com (mathmathou)]]></author>
			<pubDate>Tue, 14 Feb 2017 19:33:29 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=18967#p18967</guid>
		</item>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=18964#p18964</link>
			<description><![CDATA[<p>if&nbsp; I run this in sqlitestudio I get the correct answer</p><div class="codebox"><pre><code>SELECT FreightRateChart.FlatRate 
FROM FreightRateChart 
LEFT OUTER JOIN TruckCompanies ON FreightRateChart.id_TruckCompanies = TruckCompanies.id 
LEFT OUTER JOIN Products ON FreightRateChart.id_Products = Products.id 
LEFT OUTER JOIN LoadOrigin ON FreightRateChart.id_LoadOrigin = LoadOrigin.id 
LEFT OUTER JOIN LoadDestination ON FreightRateChart.id_LoadDestination = LoadDestination.id 

WHERE TruckCompanies.Name LIKE &#039;Trucking Co4&#039;
  AND Products.Name LIKE &#039;Gas&#039; 
  AND LoadOrigin.Name LIKE &#039;Origin1&#039; 
  AND LoadDestination.Name LIKE &#039;Dest1&#039;

ORDER BY FreightRateChart.id DESC LIMIT 1;</code></pre></div><p>but when I try in mvd with<br /></p><div class="codebox"><pre><code>flatrate := SQLExecute(&#039;SELECT FlatRate FROM FreightRateChart LEFT OUTER JOIN TruckCompanies ON FreightRateChart.id_TruckCompanies = TruckCompanies.id LEFT OUTER JOIN Products ON FreightRateChart.id_Products = Products.id LEFT OUTER JOIN LoadOrigin ON FreightRateChart.id_LoadOrigin = LoadOrigin.id LEFT OUTER JOIN LoadDestination ON FreightRateChart.id_LoadDestination = LoadDestination.id WHERE TruckCompanies.Name &#039;&#039;&#039; +Form1.truckCombo.Text+ &#039;&#039;&#039; AND Products.Name &#039;&#039;&#039; +Form1.productCombo.Text+ &#039;&#039;&#039; AND LoadOrigin.Name &#039;&#039;&#039; +Form1.originCombo.Text+ &#039;&#039;&#039; AND LoadDestination.Name &#039;&#039;&#039; +Form1.destinationCombo.Text+ &#039;&#039;&#039; ORDER BY FreightRateChart.id DESC LIMIT 1; &#039;);</code></pre></div><p>flatrate never gets set to a value</p><p>I don&#039;t think I am understanding the method of getting the variables into the sqlquery properly.</p>]]></description>
			<author><![CDATA[null@example.com (timlitw)]]></author>
			<pubDate>Tue, 14 Feb 2017 14:41:01 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=18964#p18964</guid>
		</item>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=18963#p18963</link>
			<description><![CDATA[<p>I had figured out the limit 1,&nbsp; <br /> I am struggling with how to take that long sql and wrap it into a SQLExecute with the 4 variables needed to get the two results.</p><p>Also can I get them both with one call to the database or do I need to run it twice.</p><p>here is the project the freight table shows the sql working to filter the list for editing etc.</p>]]></description>
			<author><![CDATA[null@example.com (timlitw)]]></author>
			<pubDate>Tue, 14 Feb 2017 12:45:29 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=18963#p18963</guid>
		</item>
		<item>
			<title><![CDATA[Re: sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=18958#p18958</link>
			<description><![CDATA[<p>Hello.</p><br /><p>Try to add keyword LIMIT 1</p><br /><div class="codebox"><pre><code>....
WHERE TruckCompanies.Name LIKE &#039;%{TruckSearch}%&#039;
  AND Products.Name LIKE &#039;%{ProdSearch}%&#039;
  AND LoadOrigin.Name LIKE &#039;%{OriginSearch}%&#039;
  AND LoadDestination.Name LIKE &#039;%{DestSearch}%&#039;

ORDER BY FreightRateChart.id DESC

LIMIT 1;</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (DriveSoft)]]></author>
			<pubDate>Tue, 14 Feb 2017 11:27:14 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=18958#p18958</guid>
		</item>
		<item>
			<title><![CDATA[sql in code question]]></title>
			<link>https://myvisualdatabase.com/forum/viewtopic.php?pid=18944#p18944</link>
			<description><![CDATA[<p>this sql query wroks to fill and filter a table grid&nbsp; - now in a form I need to<br />choose Truck company, Products, Origin and Destination&nbsp; and then get the Flat rate and Surcharge&nbsp; from the database - if there are multiple that match I just need the newest one.</p><div class="codebox"><pre><code>SELECT FreightRateChart.id,
       TruckCompanies.Name,
       Products.Name,
       LoadOrigin.Name,
       LoadDestination.Name,
       FreightRateChart.FlatRate,
       FreightRateChart.Surcharge,
       strftime ( &#039;%m/%d/%Y&#039;,FreightRateChart.Updated)
From FreightRateChart

LEFT OUTER JOIN TruckCompanies ON FreightRateChart.id_TruckCompanies = TruckCompanies.id
LEFT OUTER JOIN Products ON FreightRateChart.id_Products = Products.id
LEFT OUTER JOIN LoadOrigin ON FreightRateChart.id_LoadOrigin = LoadOrigin.id
LEFT OUTER JOIN LoadDestination ON FreightRateChart.id_LoadDestination = LoadDestination.id

WHERE TruckCompanies.Name LIKE &#039;%{TruckSearch}%&#039;
  AND Products.Name LIKE &#039;%{ProdSearch}%&#039;
  AND LoadOrigin.Name LIKE &#039;%{OriginSearch}%&#039;
  AND LoadDestination.Name LIKE &#039;%{DestSearch}%&#039;

ORDER BY FreightRateChart.id DESC;</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (timlitw)]]></author>
			<pubDate>Mon, 13 Feb 2017 21:52:36 +0000</pubDate>
			<guid>https://myvisualdatabase.com/forum/viewtopic.php?pid=18944#p18944</guid>
		</item>
	</channel>
</rss>
