What is the difference between using trbegin - commit to write or rewrite to a table versus using an update with statement

SOLVED

I'm curious to understand the difference between using a trbegin - commit - rollback sequence to update a value(s) on a table vs. using an Update With statement .

the Trbegin statement would look like this:

Trbegin [TBL]

If !fstat
Write [F:TBL]
Commit
Else
Rollback
Endif

the Update With statement would look like this:

Update [TBL] where [TBL]FIELD = FIELD with [TBL]COLUMN = VALUE

When would one be preferred over the other?

Please accept my apologies if I've posted on the wrong forum or place Sage City.

Thanks,

Jason

  • 0
    SUGGESTED

    Hello.

    Overall, Trbegin is a safer bet, since you can Rollback the transaction if something goes wrong, especially if you want to update multiple tables, much like the "begin transaction - commit/rollback" on SQL Server.

    Regards,

    Pedro Rodrigues.

  • 0 in reply to Pedro R

    Hi Pedro,

    Thank you for the response. i understand the safety with rollback, i should've stated that in the question.

    Is there a performance difference between the two? is Update With preferred in some situations over Trbegin - Commit - Rollback.

    The reason for my question, is that i'm developing an entry point to FUNSTOP that updates records in STOLOT, STOLOTFCY, and STOCK. Right now i have each of these using Update With, which works, but I'm wondering if Trbegin is better here.

    How would Trbegin work in an entry point function like FUNSTOP?

  • 0 in reply to junknown

    Hello,

    If you need to update several records and especially if you do not need to read each of these records, the use of "Update" is recommended because it is actually less expensive.

    This does not prevent you from using "Trbegin" which is essential for any correctly written code with the use of the "adxlog" system variable in order to know if a transaction is not already in progress as well as the "adxuprec" system variable which allows to know the number of records updated by the "Update".

    Finally, if you have to write a good number of records it is always better to go through "WriteB" which is too often forgotten.

    Regards,

    Laurent

  • 0 in reply to L.V. Expertise X3

    Hi Laurent,

    Thank you. By using Update With, the responsibility of managing 'in progress transactions' falls on the timing of the batch?

    If the batch runs in the middle of a work day, the possibility increases that a record being updated may be in use by an active use.

    In this case, the value ultimately stored in the database is the value that was saved by whoever locked the record first?

    If i understand correctly, by scheduling this batch to run during 'off-hours' the possibility of record locks by users will decrease and therefore reduce the possibility of that occurring?

    I've never heard of WriteB, i'll take a look at that.

    Regards,

    Jason

  • +1 in reply to junknown
    verified answer

    There are two types of locks.

    The symbol which is used when someone opens a customer, invoice, sale, etc...

    It's just a logical lock to prevent other users from editing this entry because, maybe, the first person can do it. It doesn't lock anything if you want to update the lines with a 4GL code or a SQL request for instance.

    The second lock is a physical one. It is enabled when there is actualy an operation being processed in the database. It is commonly really quick.

    I can't details every possibilities but this 2 operations are the most common ones.

    There is also readlock and everything with "lock" in it ;-) that can be triggered manually and that can run for a moment.

    What I want to point is that you don't have too much to worry about locked entries. You just need to handle this by writing an "Else" instruction in case your "If !fstat" doesn't work.

    If you use "Trbegin" or not and there is a lock in progress on your entry, it will not be updated and the values will be the ones edited by the process who locked first.

    And of course, during the night you will face less disappointments. Grinning

    Hope it will help.

  • 0 in reply to L.V. Expertise X3

    Thank you Laurent. This is a very helpful description of the lock and write. The manuals i have describe this but it's much clearer and more succinct here.

    This answers my question as i needed to confirm if Update With is viable in the use case described. It appears to be a viable and acceptable way to handle the project I'm working on.

    Thanks again,

    Jason