Both perform DML, but they differ in error handling.
Statement form:
apex insert myList; // throws DmlException if any record fails; no records committed
All-or-nothing: if one record fails validation, the whole list is rolled back.
Database method form:
apex Database.SaveResult[] results = Database.insert(myList, false);
The second argument allOrNone controls behaviour:
true(default) — same as statement form: all-or-nothing.false— partial success: each record either succeeds or fails individually. Records that succeed get committed; failed ones return errors in the SaveResult.
You then iterate over SaveResult[] to inspect:
apex for (Integer i = 0; i < results.size(); i++) { if (!results[i].isSuccess()) { for (Database.Error err : results[i].getErrors()) { System.debug('Record ' + i + ' failed: ' + err.getMessage()); } } }
When to use each:
- Statement form — when one failure should reject everything (typical for tightly-related records).
- Database method, allOrNone=false — when you can tolerate partial success and want to log/handle individual failures (typical for batch loads, integrations).
Equivalent variants exist for Database.update, Database.upsert, Database.delete, Database.undelete.
