Manipulación de datos
Puede escribir código para manipular los datos almacenados en la base de datos.
- Utilice un método
selectpara declarar los datos que se deben modificar. - Un método
insertse utiliza para agregar uno o más registros a una tabla. - Un método
updatese utiliza para modificar el registro actual en los campos del sistema apropiados. - Utilice una cláusula
wherepara especificar una condición que el métodoupdateprobará a medida que procesa cada fila de la tabla. - Un método
deletese utiliza para eliminar registros de una tabla. Antes de utilizar un métodoupdateodelete, debe usar un métodoselectpara definir los datos que se deben actualizar o eliminar. - Para seleccionar varios registros o campos, inicialmente utilizará un método
selecty luego usará un métodonextpara 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; }