Page 1 of 1

Problem with SQL Server Computed Fields

Posted: Sat 11 Nov 2017 20:14
by Mahmood_M
I have a problem with Computed Fields in SQL Server
Steps to reproduce the problem :
1. Create a new Table in SQL Server and add some fields to it
2. Create a field ( name it "MyField" ) and set a formula in the "Computed Column Specification" section of field properties like this : ([ID] + 1000) - ( ID is the auto-increment field )
3. Create a new project in Delphi, add a TUniConnection and a TUniTable
connect Table and add fields to it ( add a DBGrid to see the values )
4. Insert a record in the table :

Code: Select all

UniTable1.Insert;
UniTable1.FieldByName('FName').AsString := '';
UniTable1.FieldByName('FFamily').AsString := '';
...
UniTable1.Post;
* not assign any value to Computed field (MyField);
Now the problem is MyField has no value in DBGrid untile TUniTable reactivated !
If you try to Edit Table like this :

Code: Select all

UniTable1.Edit;
...
UniTable1.Post;
an error shown as "Record was changed by another user"

All Computed fields has this problem, what goes wrong ?

Re: Problem with SQL Server Computed Fields

Posted: Sun 12 Nov 2017 13:28
by Mahmood_M
Any suggestion ?

Re: Problem with SQL Server Computed Fields

Posted: Mon 13 Nov 2017 13:20
by Stellar
The value of the calculated fields does not change when the record is refreshed, because they are not included in the record refresh query.
Try manually setting the refresh query for the record where there are Computed Columns:

UniTable.SQLRefresh.Text := ' SELECT FName, FFamily, MyField FROM Table WHERE KeyField = :KeyField ';

After changing the data in the database, you need to refresh the record in the dataset. You can refresh the record either manually TUniTable.RefreshRecord, or automatically by setting the RefreshOptions property of the TUniTable component when executing the refresh.
For example, refresh the record after inserting and updating:
UniTable.RefreshOptions: = [roAfterInsert, roAfterUpdate];

Refreshing the record after the change will solve the issue "Record was changed by another user".

Re: Problem with SQL Server Computed Fields

Posted: Tue 14 Nov 2017 14:15
by Mahmood_M
Problem solved, thanks

Re: Problem with SQL Server Computed Fields

Posted: Tue 14 Nov 2017 15:03
by Stellar
Glad to see that the issue was resolved.
Feel free to contact us if you have any further questions about our products.