﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » Business Objects and Data Access (How do I?)  » Query Sql Count and Business Object</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Fri, 22 May 2026 11:27:32 GMT</lastBuildDate><ttl>20</ttl><item><title>Query Sql Count and Business Object</title><link>http://forum.strataframe.net/FindPost32470.aspx</link><description>Hello&lt;br/&gt;&lt;br/&gt;I have a simple query to count in a sales graph and i group by seller&lt;br/&gt;&lt;br/&gt;but Strata Bo ask me all primary key in my view and my query is not disctint or grouping.&lt;br/&gt;&lt;br/&gt;Can you tell me , how you start your query with count(*) and some group by on Stratraframe&lt;br/&gt;to learn and reproduce this.&lt;br/&gt;&lt;br/&gt;thanks&lt;br/&gt;Olivier,</description><pubDate>Thu, 27 Feb 2014 08:14:42 GMT</pubDate><dc:creator>Olivier</dc:creator></item><item><title>RE: Query Sql Count and Business Object</title><link>http://forum.strataframe.net/FindPost32477.aspx</link><description>Hi Olivier,&lt;br/&gt;You have to change the way you are thinking from VFP to a SQL DBA so you can build your queries better in SQL.&lt;br/&gt;&lt;br/&gt;I would build your query this way, for testing purpose set the value of the ProductName and ProductPrice parameters then comment them for production.&amp;nbsp; What makes this query generic in a way that could accept a Product Name or a Product Price is the values of the parameters which are expected as &lt;span&gt;'%'&lt;/span&gt; or zero, and in this case since you are using the "LIKE" attribute, then we will test for the string &lt;span&gt;'%' to test for empty, or you can create whatever condition you want to test as empty for the ProductName.&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span&gt;[codesnippet]&lt;br/&gt;-- &lt;/span&gt;&lt;span&gt;&lt;span&gt;spGetProductByPriceOrName&lt;br/&gt;&lt;/span&gt;declare @ProductName varchar(30)&lt;br/&gt;declare @ProductPrice DECIMAL&lt;br/&gt;&lt;br/&gt;set @ProductName = 'm.dupont'&lt;br/&gt;set @ProductPrice = 120000&lt;br/&gt;&lt;br/&gt;SELECT &lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.*&lt;br/&gt;FROM&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Product p&lt;br/&gt;WHERE&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (p.Name LIKE @ProductName OR @ProductName = '%') AND&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (p.Price &amp;gt;= @ProductPrice OR @ProductPrice = 0)&lt;br/&gt;[/codesnippet]&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;In your VB program you would have a method or function like this:&lt;br/&gt;&lt;span&gt;[codesnippet]&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public Sub MyName(ProductName As String, ProductPrice As Decimal)&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Using cmd As New SqlClient.SqlCommand()&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Create the params&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cmd.CommandTimeout = 0&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cmd.CommandType = CommandType.StoredProcedure&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cmd.CommandText = "dbo.spGetProductByPriceOrName"&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cmd.Parameters.AddWithValue("@ProductName", ProductName.Trim + "%").SqlDbType = SqlDbType.VarChar&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cmd.Parameters.AddWithValue("@ProductPrice", ProductPrice).SqlDbType = SqlDbType.Int&lt;br/&gt;&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Me.ExcecuteScalar(cmd)&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Using 'cmd&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;br/&gt;[/codesnippet]&lt;/span&gt;</description><pubDate>Thu, 27 Feb 2014 08:14:42 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: Query Sql Count and Business Object</title><link>http://forum.strataframe.net/FindPost32476.aspx</link><description>Hello Edhy&lt;br/&gt;&lt;br/&gt;What do you think about this ! i want to build a dynamic query !&lt;br/&gt;and do you think it's secure ?&lt;br/&gt;&lt;br/&gt;&lt;span&gt;[code]&lt;/span&gt;&lt;br/&gt;declare @cName varchar(30);&lt;br/&gt;declare @cPrice varchar(12);&lt;br/&gt;declare @cCondition varchar(max);&lt;br/&gt;declare @MyQuery varchar(max)&lt;br/&gt;&lt;br/&gt;set @cName = 'm.dupont';&lt;br/&gt;set @cPrice = '120000';&lt;br/&gt;&lt;br/&gt;IF @cName &amp;lt;&amp;gt;''&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set @cCondition = 'name like '+@cName+'%';&lt;br/&gt;&lt;br/&gt;IF @cPrice &amp;lt;&amp;gt;''&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set @cCondition = 'price&amp;gt;='+@cPrice;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;SET @MyQuery = 'select * from product where ' + @cCondition&lt;br/&gt;EXEC (@MyQuery)&lt;br/&gt;&lt;br/&gt;&lt;span&gt;[/code]&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;Do you see the way what i need ?&lt;br/&gt;&lt;br/&gt;thanks&lt;br/&gt;Olivier</description><pubDate>Thu, 27 Feb 2014 02:48:42 GMT</pubDate><dc:creator>Olivier</dc:creator></item><item><title>RE: Query Sql Count and Business Object</title><link>http://forum.strataframe.net/FindPost32475.aspx</link><description>Hello Edhy,&lt;br/&gt;&lt;br/&gt;I ll test tomorrow (it's night on france),&lt;br/&gt;but i have already build a view before, and i m no way cause the bo ask primary key and i don't put this,&lt;br/&gt;cause i want to group a record.&lt;br/&gt;&lt;br/&gt;may be it's override primary key the solution,&lt;br/&gt;&lt;br/&gt;i test and i come back to tell you !&lt;br/&gt;&lt;br/&gt;thanks &lt;br/&gt;Olivier</description><pubDate>Wed, 26 Feb 2014 13:04:11 GMT</pubDate><dc:creator>Olivier</dc:creator></item><item><title>RE: Query Sql Count and Business Object</title><link>http://forum.strataframe.net/FindPost32474.aspx</link><description>Hi Olivier,&lt;br/&gt;&lt;br/&gt;Here is a sample of a query I used to test before creating the stored procedures:&lt;br/&gt;&lt;span&gt;[codesnippet]&lt;br/&gt;DECLARE @StartDate AS DATE = '1/1/2013'&lt;br/&gt;DECLARE @EndDate AS DATE = '12/31/2013'&lt;br/&gt;DECLARE @PK_InsuranceCompany AS INT = 0&lt;br/&gt;&lt;br/&gt;SELECT&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; v.CompanyName,&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SUM(v.BrokerCommissionValue) AS BrokerCommissionvalue&lt;br/&gt;FROM&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; vCompanyPayments v&lt;br/&gt;WHERE&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CAST(v.ReceiptDate AS DATE) &amp;gt;= @StartDate AND CAST(v.ReceiptDate AS DATE) &amp;lt;= @EndDate AND&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; BrokerCommissionvalue &amp;gt; 0 AND&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (v.PK_InsuranceCompany = @PK_InsuranceCompany OR @PK_InsuranceCompany = 0)&lt;br/&gt;&lt;br/&gt;GROUP BY&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; v.CompanyName&lt;br/&gt;&lt;br/&gt;ORDER BY&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; v.CompanyName&lt;br/&gt;[/codesnippet]&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;Notice how I test for the value of the parameter &lt;span&gt;@PK_InsuranceCompany to be = 0 in the WHERE condition, in this case in my VB application I pass either a real value greater than zero or just zero and the I will get all records for that Insurance Company PK or for all.&lt;br/&gt;You can use similar code to replace your VFP coding and in VB you can control how to pass the parameters to your SQLCommand.&lt;br/&gt;&lt;/span&gt;</description><pubDate>Wed, 26 Feb 2014 13:03:08 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: Query Sql Count and Business Object</title><link>http://forum.strataframe.net/FindPost32473.aspx</link><description>thanks michel,&lt;br/&gt;&lt;br/&gt;i ll check , the override primary key option.&lt;br/&gt;&lt;br/&gt;Can you see the way to build an procedure stored like this ?&lt;br/&gt;to get a condition to put in the Where ?&lt;br/&gt;(i give you a code in vfp cause i dont' get a vb code)&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;span&gt;[code]&lt;br/&gt;If ! empty(m.nNBPIECES) &lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m.cCondition = m.cCondition + " .AND. CLI_PIECMN&amp;lt;="+Alltr(str(m.nNBPIECES))&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Endif&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; If ! empty(m.nSURFACE)&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; m.cCondition = m.cCondition + " .AND. CLI_SURF&amp;lt;="+Alltr(str(m.nSURFACE+(m.nSURFACE*M.XPCKORL1/100)))&lt;br/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Endif&lt;br/&gt;&lt;br/&gt;select myfield from mytable where 1=1 and &amp;amp;cCondition&lt;br/&gt;&lt;br/&gt;[/code]&lt;br/&gt;&lt;br/&gt;it's the last line i don't get in tsql for my proc stor !&lt;br/&gt;&lt;br/&gt;thanks&lt;br/&gt;Olivier&lt;br/&gt;&lt;/span&gt;</description><pubDate>Wed, 26 Feb 2014 12:54:41 GMT</pubDate><dc:creator>Olivier</dc:creator></item><item><title>RE: Query Sql Count and Business Object</title><link>http://forum.strataframe.net/FindPost32472.aspx</link><description>Hi Olivier, Michel,&lt;br/&gt;&lt;br/&gt;Michel is right, sometimes a view and a stored procedure to fill the view is what is needed, also if all the aggregated fields are in the view the SQL to query the view may be pretty simple no needing to have a stored procedure for it.&lt;br/&gt;&lt;br/&gt;My preference is to try to get all calculations done at the SQL Server level so I can simply use the views in my reports or graphs with a SF Business Binding Source as clean as possible.</description><pubDate>Wed, 26 Feb 2014 12:52:41 GMT</pubDate><dc:creator>Edhy Rijo</dc:creator></item><item><title>RE: Query Sql Count and Business Object</title><link>http://forum.strataframe.net/FindPost32471.aspx</link><description>Hi Olivier,&lt;br/&gt;&lt;br/&gt;If you don't want to use the agregating functions provided in your report engine, it is possible (I often prefer to agregate on the SQL Server).&lt;br/&gt;&lt;br/&gt;here&amp;nbsp;is a&amp;nbsp;solution (there&amp;nbsp;are also&amp;nbsp;some other ways):&lt;br/&gt;&lt;br/&gt;&lt;ol&gt;&lt;li&gt;create a view on the server with the count(*) and grouping you need, map a BO on that view, you have the structure (don't forget to enforce a pseudo primary key on the BO - look at "override&amp;nbsp;primary key specification" in the Advanced otions of the BO, choose the columns that may identify a line)&amp;nbsp;&lt;/li&gt;&lt;li&gt;if you need some parameters, create a stored procedure resulting in the same strcuture, having th parameters you need&lt;/li&gt;&lt;li&gt;fill your BO with the stored procedure&lt;/li&gt;&lt;/ol&gt;That's all, guy!&lt;br/&gt;&lt;br/&gt;&lt;span&gt;[hr]&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span&gt;Salut Olivier,&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span&gt;Si tu ne veux pas utiliser les fonctions d'aggrégation de ton moteur de rapport, c'est faisable (je préfère souvent faire les agrégations sur le serveur SQL).&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;span&gt;Voilà une solution (il y en a d'autres):&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;ol&gt;&lt;li&gt;&lt;span&gt;tu crées une vue sur le serveur, avec ton count(*) et tes group by, tu mappes un BO sur cette vue, ça te donne ta structure (n'oublie pas de forcer une pseudo PK sur le BO - regarde "override primary key specification" dans les options avancées du BO, tu choisis une combinaison&amp;nbsp;de colonnes qui peuvent servir d'identifiant unique pour une ligne)&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;si tu as besoin de paramètres, tu crées une procédure stockée qui te donne le résultat voulu, avec les paramètres nécessaires&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span&gt;tu remplis ton BO avec la procédure stockée&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span&gt;Et c'est fini, mon ami!&lt;/span&gt;</description><pubDate>Wed, 26 Feb 2014 12:42:09 GMT</pubDate><dc:creator>Michel Levy</dc:creator></item></channel></rss>