Skip to main content
April 21, 2024
Solved

Execute sql on data table

  • April 21, 2024
  • 4 replies
  • 0 views

Can I execute an sql statement against a data table created in a business rule?  I created a table to use in a data set business rule, which returns data to the data adapter.   

e.g. if I created dt1 using : Dim dt1 As DataTable = BRApi.Database.ExecuteSql(dbConnApp, strSQL.ToString, True)

Can I run a query against dt1, doing something like "select * from dt1" or in other words can I get the tablename for dt1 and then execute a query?

Best answer by AndreaF

The datatable exists within the data set business rule, so yes you can run a query against it, but within its limit of existence, as for any other variable, i.e. you cannot refer to it from other business rules or outside the function or loop where it is defined. However, even within its limit of existence, I believe you cannot refer to it with the "select * from dt1" string. You need to use the appropriate function of the BRApi.Database (I don't remember which one at the moment), and pass the table and where/order by etc. as parameters.

4 replies

AndreaFAnswer
April 21, 2024

The datatable exists within the data set business rule, so yes you can run a query against it, but within its limit of existence, as for any other variable, i.e. you cannot refer to it from other business rules or outside the function or loop where it is defined. However, even within its limit of existence, I believe you cannot refer to it with the "select * from dt1" string. You need to use the appropriate function of the BRApi.Database (I don't remember which one at the moment), and pass the table and where/order by etc. as parameters.

April 21, 2024

The DataTable is held in memory. Once you have an instance of it, you can call various methods and properties to look at its data; see this Microsoft page for some examples. Once the rule has run, the DataTable object is automatically destroyed.

If you want to retrieve the dataset produced by an adapter, in some other rule you can use BRApi.Dashboards.Process.GetAdoDataSetForAdapter, which will effectively run the adapter and give you back the result as DataSet object (for an introduction to DataSets, see this other Microsoft page). The DataSet will contain the result DataTable(s).

 

 

 

.

PB5Author
April 22, 2024

Great - thanks for the information - I will use what I can from this - I don't necessarily want to go too deep into customization and fancy script, but I'll evaluate the options.

April 22, 2024

AndreaF - Yes Data table are in memory and you can manipulate by copying to another DTL or you can use the existing DTL as well. See below an example 

									WFICTable1 = WFICTable.Copy()
											
												Dim distinctValues = WFICTable1.AsEnumerable() _
												.Where(Function(row) row.Field(Of String)("Account") <> "Difference")  _
											    .GroupBy(Function(row) New With {
											        Key.Column1 = row.Field(Of String)("Entity"),
											        Key.Column2 = row.Field(Of String)("IC")
											    }) _
											    .Select(Function(Group) New With {
											        .Column1 = Group.Key.Column1,
											        .Column2 = Group.Key.Column2,
											        .OtherColumn1 = Group.First().Field(Of String)("Account"),
											        .OtherColumn2 = Group.First().Field(Of String)("PartnerCurr"),
													.OtherColumn3 = Group.First().Field(Of Decimal)("RptCurrAmount"),
													.OtherColumn4 = Group.First().Field(Of Decimal)("PrimaryCurrAmount"),
													.OtherColumn5 = Group.First().Field(Of Decimal)("PartnerCurrAmount")
											    })

 

 

PB5Author
April 23, 2024

Thanks Krishna - I'll check it out - looks like your doing something on IC mismatch reporting - that's what I'm working on with this issue too.

April 23, 2024

PB - Yes but the concept is the same you can try with your SQL table column. Hope this helps.