add calcfield to TQuery - sql

I found this code to create a calc field to a TADOTable in Delphi somewhere ...
.....
procedure TfrmMain.ABSTable1CalcFields(DataSet: TDataSet);
begin
with ABSTable1 do
FieldByName('cost').AsFloat := FieldByName('price').AsFloat *
FieldByName('quantity').AsInteger;
// add new field cost as Price * quantity !!!!
end;
end.
Inside my app i create a TADOQuery at rum time like
try
Fquery.sql.clear;
Fquery.sql.AddStrings(Amemo.lines);
Fquery.Open;
.....
finally
end;
How to add more calc fields to my Query derived from the first code Fragment ?

I think the only way you can easily do this is by creating a set of persistent TFields in the IDE (or do the equivalent creation of them in code before you open the dataset). Otherwise, when you call Open on the dataset, IIRC that will call BindFields and that - unless the dataset already has a set of TFields - will create a set of dynamic TFields which will last as long as the dataset is open, but will not include any calculated fields.
By the time BindFields has been called, it's too late to add any more, so you have to set them up beforehand or not at all.

Related

Setting the value of an Enum field in a temporary record

Total newbie in AL here; I'm experimenting with automated testing in Dynamics BC for a new deployment. As a very basic test, I'd like to simply create a new Item record in the Cronus test database and validate each field. I'm running into trouble when I try to select the value for an enum field. Here's the code I'm using:
Procedure AddGoodItem()
// [Given] Good item data
var
recItem: Record Item Temporary;
Begin
recItem."Description" := 'zzzz';
recItem.validate("Description");
recItem.Type := recItem.Type::Inventory;
recItem.Validate(Type);
recItem."Base Unit of Measure" := 'EA';
recItem.Validate("Base Unit of Measure");
recItem."Item Category Code" := 'FURNITURE';
recItem.validate("Item Category Code");
End;
When I run this in Cronus, I get the error:
You cannot change the Type field on Item because at least one Item Journal Line exists for this item.
If I comment the Type lines out, the process runs successfully.
Given that this is a temporary record, it shouldn't have any Item Journal Lines, should it? What am I missing?
The code in the OnValidate trigger still runs, even if you have marked the Record variable as temporary. In addition temporary is not inherited to the underlying variables meaning any Record variables used in the OnValidate trigger are not temporary (unless marked as such).
There are two options:
Leave out the line recItem.Validate(Type);, if the code run by the OnValidate trigger is not relevant in this case.
Replace the line recItem.Validate(Type); with your own clone of the code from the OnValidate trigger and then remove the unneeded parts.

error in google datastudio on using custom query

I am using a simple sql block of statements to execute and return a set of results in big query, This is working fine in big query and getting the results,I need to export this data to data studio, so in data studio i use bigquery as connector and select the project and custom query and in that I paste the contents below:
Declare metricType String;
SET metricType="compute.googleapis.com/instance/cpu/utilization";
BEGIN
IF (metricType="compute.googleapis.com/instance/cpu/usage_time")
THEN
SELECT m.value as InstanceName,metric.type as metricType,point.value.double_value as usagetime,point.interval.start_time as StartTime,point.interval.end_time as EndTime,h.value as instance_id FROM `myproject.metric_export.sd_metrics_export_fin`, unnest(resource.labels) as h,unnest(metric.labels) as m where metric.type='compute.googleapis.com/instance/cpu/usage_time' and h.key="project_id";
ELSE IF (metricType="compute.googleapis.com/instance/cpu/utilization")
THEN
SELECT m.value as InstanceName,metric.type as metricType,point.value.double_value as utilizationrate,point.interval.start_time as
StartTime,point.interval.end_time as EndTime,h.value as instance_id FROM `myproject-.metric_export.sd_metrics_export_fin`,unnest(resource.labels) as h,unnest(metric.labels) as m where metric.type='compute.googleapis.com/instance/cpu/utilization' and h.key="project_id";
END IF;
END IF;
END;
but after click "ADD" button i get the below error:
I am not sure what is this error about? I have not used any stored procedure and I am just pasting it as custom query.
Also If I try to save the results of the BigQuery into a view from the Bigquery console results pane, it gives me the error,
Syntax error: Unexpected keyword DECLARE at [1:1]
I am extremely new to datastudio and also to bigquery. Kindly Help thanks
First, I would like to make some considerations about your query. You are using Scripting in order to declare and create a loop within your query. However, since you declare and set the metricsType in the beginning of the query, it will never enter in the first IF. This happens because the value is set and it is not looping through anything.
I would suggest you to use CASE WHEN instead, as below:
SELECT m.value as InstanceName,metric.type as metricType,
CASE WHEN metric.type = #parameter THEN point.value.double_value ELSE 0 END AS usagetime,
CASE WHEN metric.type = #parameter THEN point.value.double_value ELSE 0 END AS utilizationrate,
point.interval.start_time as StartTime,point.interval.end_time as EndTime,h.value as instance_id
FROM `myproject.metric_export.sd_metrics_export_fin`, unnest(resource.labels) as h,unnest(metric.labels) as m
WHERE metric.type=#parameter and h.key="project_id";
Notice that I am using the concept of Parameterized queries. Also, for this reason this query won't work in the console. In addition, pay attention that whem you set the #parameter to "compute.googleapis.com/instance/cpu/utilization", it will have a non-null column with the usagetime and a null column named utilizationrate.
Secondly, in order to add a new data source in DataStudio, you can follow this tutorial from the documentation. After, selecting New Report, click on the BigQuery Connector > Custom Query> Write your Project id, you need to click in ADD PARAMETER (below the query editor). In the above query, I would add:
Name: parameter
Display name: parameter
Data type: text
Default value: leave it in blank
Check the box Allow "parameter" to be modified in reports. This means you will be able to use this parameter as a filter and modify its value within the reports.
Following all the steps above, the data source will be added smoothly.
Lastly, I must point that if your query ran in the Console you are able to save it as a view by clicking on Save view, such as described here.

Binding a foreign key when adding a new value to the master-detail table Delphi

There are 2 tables linked by master-detail. When adding a new value to the detail table, the foreign key selected from the master table is not bound.
The M-D connection itself is performed on the form using two Dblookupcombobox and DataSource, ADOQuery for each, respectively.
enter image description here
Using the [ + ] buttons, new values are added that are not present in the combobox. But the problems start at the second [ + ] (aka detail), when creating a new line, you need it to bind the foreign key from the previous LookUpComboBox (Master). Button code of the second button [+]:
begin
Form4.ADOQuery1.SQL.Clear;
Form4.ADOQuery1.SQL.Add('Select City from City WHERE City='+#39+Form5.DBEdit1.Text+#39); //checking for duplicates
Form4.ADOQuery1.Open;
if Form4.ADOQuery1.IsEmpty then
begin
Form4.Query_city.FieldByName('City').AsString := Form5.DBEdit1.Text; //The PROBLEM is SOMEWHERE HERE! It Adds a new value without binding the foreign key
Form4.Query_city.Open;
Form4.Query_city.Post;
MessageBox(Handle, 'New data entered','Adding a new value',MB_ICONINFORMATION);
end
else
begin
Form4.Query_spec.Cancel;
Form4.ADOQuery1.Cancel;
MessageBox(Handle,PChar(''+Form5.DBEdit1.text+' already on the list!'),'Error',MB_ICONWARNING);
end;
end;
The new value is written to DBEdit1. It has a corresponding binding to tables.
So How i can insert field with with the corresponding foreign key?
You are making this unneccessarily difficult because of the way your
code is structured. Try something like this instead:
Open ADOQuery1, SELECTing its entire contents, with e.g.
procedure TForm4.OpenCitiesTable;
begin
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('Select City from City');
ADOQuery1.Open;
end;
and leave it open while the user does whatever operations lead to wanting to add a new city.
Then, when the users wants to add a city, call this procedure, e.g. supplying the City
value from Form5.DBEdit1.Text.
procedure TForm4.AddCity(ACity : String);
begin
ACity := Trim(ACity); // remove any leading or trailing blanks
// Do any formatting checks on ACity here, re.g convert it to Proper case
// check for adding a duplicate
if ADOQuery1.Locate('City', ACity, []) then begin
ShowMessageFmt('%s is already in the City table', [ACity]);
Exit;
end;
try
ADOQuery1.Insert;
ADOQuery1.FieldByName('City').AsString := ACity;
finally
ADOQuery1.Post;
// at this point you might want to refresh the contents of whatever source
// you are populating Form5.DBEdit1.Text from
end;
end;
I assume you can adjust the code relating to TForm4.Query_spec yourself;
Btw, you might want to consider using a TDBLookUpComboBox instead of your DBEdit1.

Old and new values in Oracle Form

I'm working with Oracle Forms. I have a field named SOLD_TO_CUST_PARTY_NAME. I have to execute a process if I detect a change in the field's value. I tried using when_validate, but it's executed even if you just click the field and move to another field (validation ALWAYS occurs whether you change the value or not). Is there anyway I can check :old and :new or something similar to execute a process only if the field is modified?
EDIT:
Can't use personalizations. It has to be done with pl/sql.
There is a property called database value that let you check if the field has been modified and if it has not you just have to exit the validation trigger.
Ex.
BEGIN
IF :BLOCK.ITEM = GET_ITEM_PROPERTY('BLOCK.ITEM', database_value) THEN
RETURN;
END IF;
/* VALIDATION */
END;

how to create Delphi 4 structure to map column names in XLS to column names in SQL

I have an EXCEL sheet.it has various form-fields that are named as
“smrBgm133GallonsGross/ smrBgm167GallonsGross “
and
“smrEgm133GallonsGross/ smrEgm167GallonsGross “
I added to the XCEL sheets 2 new form fields named
smrBgm167GrosGalnsDA/smrEgm167GrosGalnsDA
The above additons I made in EXCEL should ACTUALLy be named as
`smrBgm229GallonsGross/smrEgm229GallonsGross` because. This is a MUST for the Delphi application to function properly.
This Delphi-4 application extracts , and vewrifys the form DATA in tandem with the DB.
My Delphi-4 application works (checks/inserts/retrieves) so that current months beginning gallon of milk “bgm229” is equal to last months ending gallon of milk “egm229” , and then throw an exception if they are different.
Excel sheets:- Bgm167GrosGalnsDA/ Egm160GrosGalnsDA
Delphi Code (DB- input/ DB- output/validation):- bgm229/ egm229
SQL 2005 DB:- bgm167DA/ egm167DA
Actually the columns I ADDED should have been named asa "smrEgm133GallonsGross/ smrEgm167GallonsGross "...I messed up in naming them and they are on the production now....
In the Delphi procedure,for the beginning inventory, the code it is
ExtractFormBgmInfo(smrMilkAvMilk, 'smrBgm133');
ExtractFormBgmInfo(smrMilkDe, 'smrBgm167');
For ending inventory the code it is
ExtractFormEgmInfo(smrMilkAvMilk, 'smrEgm133');
ExtractFormEgmInfo(smrMilkDe, 'smrEgm167');
I am adding “smrBgm229GrosGalns/smrEgm229GrosGalns” to the list
But the issue is that they are named erroneously as “smrBgm167GrosGalnsDA/ smrEgm167GrosGalnsDA” IN THE EXCEL sheets, while they are to be named as 'smrBgm229/'smrEgm229''(as is the case in the Delphi code). Hence. I added ...to the above
ExtractFormBgmInfo(smrMilkAvMilk, 'smrBgm133');
ExtractFormBgmInfo(smrMilkDe, 'smrBgm167');
ExtractFormBgmInfo(smrMilkDyedDe, 'smrBgm229');
ExtractFormEgmInfo(smrMilkAvMilk, 'smrEgm133');
ExtractFormEgmInfo(smrMilkDe, 'smrEgm167');
ExtractFormEgmInfo(smrMilkDyedDe, 'smrEgm229');
This throws an error , as smrBgm229GallonsGross /smrEgm229GallonsGross are not defined in the EXCEL sheets .So the issue is how do I convert “smrBgm167GrosGalnsDA” from Excel sheets into “smrBgm229GallonsGross” and then make my “ExtractForm” statement correct?
Please help there is an release scheduled today and got to discuss this with my superirors
What you want to do is map one string to another. You can use a simple string list for that.
// Declare the variable somewhere, such as at unit scope or as a field
// of your form class
var
ColumnNameMap: TStrings;
// Somewhere else, such as unit initialization or a class constructor,
// initialize the data structure with the column-name mappings.
ColumnNameMap := TStringList.Create;
ColumnNameMap.Values['Bgm167 GrosGalns DA'] := 'bgm229/ egm229';
// In yet a third place in your code, use something like this to map
// the column name in your input to the real column name in your output.
i := ColumnNameMap.IndexOfName(ColumnName);
if i >= 0 then
RealColumnName := ColumnNameMap.Values[ColumnName]
else
RealColumnName := ColumnName;
Later versions of Delphi have the generic TDictionary class. Use TDictionary<string, string>. The TStrings solution I outlined above will have problems if any of the column names can have equals signs in them, but you can mitigate that by changing the NameValueSeparator property.
var
ColumnNameMap: TDictionary<string, string>;
ColumnNameMap := TDictionary<string, string>.Create;
ColumnNameMap.Add('Bgm167 GrosGalns DA', 'bgm229/ egm229');
if not ColumnNameMap.TryGetValue(ColumnName, RealColumnName) then
RealColumnName := ColumnName;