Data Updation in table | DML Operation | X++ Language

 

You can use SQL statements, either interactively or in source code, to update one or more rows in a table that is stored in the database.

  1. update method  – Update the current record with the contents of the buffer. Also update the appropriate system fields.
  2. doUpdate method  – Update one row at a time. You should use the doUpdate method when the update method on the table must be bypassed.
  3. update_recordset statement  – Update multiple records in one database trip. By using the update_recordset statement, you reduce communication between the application and the database. Therefore, you help increase performance. In some situations, record set–based operations can fall back to record-by-record operations. For more information, see Conversion of operations from set-based to record-by-record.

scenario

We have a to update a row of the EmployeeTable. This table is look like below.

EmpIdEmpNameDeptId
E001Rumman AnsariD001
E002Osman Ali SkD001

Task 1: You have to update a row inside the table.

Task 2: You have to update all the rows inside the table which matches the specific conditions.

Update data in a table X++ code. Single Row

This code will update only the first row


static void DataInsertionJob(Args _args)
{ 
     EmployeeMy empdet; 
        // in this case only single row will update
        ttsbegin;
            select forupdate empdet where empdet.DeptId == "D001";
            empdet.EmpName="John";
            empdet.update();
        ttscommit;
        info("Data Updated");
}

Output of the code

EmpIdEmpNameDeptId
E001JohnD001
E002Osman Ali SkD001

Update data in a table X++ code. All rows

This code will update all the rows present in the data set which matches the conditions.


static void DataUpdateJob(Args _args)
{ 
     EmployeeMy empdet; 
       
// the below code update the all the rows
        update_recordset empdet setting
            EmpName="John"
            where empdet.DeptId == "D001";
            info("All Data Updated");
}

Output of the code

EmpIdEmpNameDeptId
E001JohnD001
E002JohnD001

Another Way you can also do




static void DataUpdateJob(Args _args)
{
    EmployeeMy empdet;
    while select empdet
        where empdet.DeptId == "D001"
    {
        empdet.EmpName = "John";
        empdet.update();
    }
    info("All Data Updated");
}

Comments