Manipulación de datos

Completados

Puede escribir código para manipular los datos almacenados en la base de datos.

  • Utilice un método select para declarar los datos que se deben modificar.
  • Un método insert se utiliza para agregar uno o más registros a una tabla.
  • Un método update se utiliza para modificar el registro actual en los campos del sistema apropiados.
  • Utilice una cláusula where para especificar una condición que el método update probará a medida que procesa cada fila de la tabla.
  • Un método delete se utiliza para eliminar registros de una tabla. Antes de utilizar un método update o delete, debe usar un método select para definir los datos que se deben actualizar o eliminar.
  • Para seleccionar varios registros o campos, inicialmente utilizará un método select y luego usará un método next para capturar el siguiente registro en una tabla.
  • Para hacer un bucle sobre muchos registros que cumplen con criterios específicos, puede usar un método while select.

La instrucción select recupera o manipula la fecha de la base de datos mediante X++. Puede usar la instrucción select para obtener un registro o campo. Puede usar otras instrucciones, como next, para capturar registros adicionales o utilizar una instrucción while select para recorrer varios registros.

Por ejemplo, la siguiente muestra de código captura todas las columnas en la primera fila de CustTable y, luego, imprime el valor en la columna AccountNum de esa fila:

CustTable custTable;
select firstonly custTable; //this is a short notation for 'select firstonly * from custTable;'  
info("AccountNum: " + custTable.AccountNum); 

A continuación, se muestran ejemplos de código para los métodos insert, update y delete. Los ejemplos utilizan una tabla MyCustomerTable ficticia que contiene campos ficticios para AccountNumber y CustomerName.

  • Método insert

    //This will insert a new record into a sample MyCustomerTable table. The only mandatory field that is set on this table is AccountNumber.
    
    private void AddCustomer()
    {
        MyCustomerTable myCustomerTable;
        ttsBegin;
             myCustomerTable.AccountNumber = "1234";
             //The new record will have the account number 1234.
             myCustomerTable.insert();
        ttsCommit;
    }
    
  • Método update

    //This will update the record where the AccountNumber field is set to 1234 by adding more information in the CustomerName field.
    
    private void UpdateCustomer()
    {
        MyCustomerTable myCustomerTable
        ttsBegin;
            select forUpdate myCustomerTable
            where myCustomerTable.AccountNumber == "1234";
            //Now we will update the CustomerName field to be Sally
            myCustomerTable.CustomerName = "Sally";
            myCustomerTable.update();
        ttsCommit;
    }
    
  • Método delete

    //This will delete the record from the MyCustomerTable table where the AccountNumber field is set to 1234.
    
    private void DeleteCustomer()
    {
        ttsBegin;
            while select forUpdate myCustomerTable
            where myCustomerTable.AccountNumber == "1234"
            {
            myCustomerTable.delete();
            }
        ttsCommit;
    }