HomeWiki
HomeWiki
  1. DB
  • Back to home
  • 2. Connectivity
  • Introduction
  • Custom Script
  • Playground
  • Database Explorer
  • Log Stream
  • Direct Communication
  • Softone
    • Getting Started
  • Datapump
    • Getting Started
    • Crons
    • Legacy Windows Support
    • SDK
      • DB
        • _executeQuery
        • _executeStoreProcedure
        • _executeInsertQuery
        • _executeInsertQueriesInTransaction
      • Import
        • _productsImport
        • _categoriesImport
        • _brandsImport
        • _attributesImport
        • _attributeSetsImport
        • _unitsImport
        • _quantityImport
        • _customersImport
        • _contactsImport
        • _callsImport
        • _eventsImport
        • _tasksImport
        • _ordersImport
        • _usersImport
        • _customerSourceTagsImport
        • _productSourceTagsImport
      • Log
        • _logDebug
        • _logInfo
        • _logWarn
        • _logError
      • Web Requests
        • _httpGetJson
        • _httpPostJson
        • _httpPutJson
        • _httpDeleteJson
        • _httpPatchJson
      • Variables
        • _saveVariable
        • _getVariable
        • _deleteVariable
      • Blob
        • _blobConnect
        • _blobExists
        • _blobGet
        • _blobFind
      • Utils
        • _encodeBase64
        • _sleep
        • _getLastSyncDate
        • _updateLastSyncDate
        • _resetLastSyncDate
  1. DB

_executeInsertQueriesInTransaction

Explanation: _executeInsertQueriesInTransaction Function#

The _executeInsertQueriesInTransaction function in JavaScript is designed to execute a series of SQL queries within a single transaction. This method ensures that either all of the queries are successfully executed, or none are applied, which is crucial for maintaining data integrity when inserting related records into a database. The function first executes a "header" query to insert a primary record and retrieves its generated ID. It then uses this ID to perform subsequent "detail" queries.

Function Signature:#

Parameters:#

queries (Array of string):
Description: An array of SQL queries to be executed. The first query in the array should be the "header" query, which is expected to generate an identity value (e.g., auto-incremented ID). The remaining queries are "detail" queries that use this identity value to relate the detail records to the header record.
Type: Array<string>
Example:

Return Value:#

Returns:
The ID of the newly inserted record from the "header" query. This ID is generated by an identity column and is used in the subsequent queries to relate them to the header record.
Type: number
Example:

Function Workflow:#

1.
Establish Connection:
The function opens a connection to the database using the connection string.
2.
Begin Transaction:
A SQL transaction is started to ensure that all queries are executed as part of a single atomic operation.
3.
Execute Header Query:
The function executes the first query in the queries array, which should be an INSERT statement that generates a new record with an identity column.
After executing the header query, it retrieves the generated identity value using a query such as SCOPE_IDENTITY() or similar, and stores this value as headerId.
4.
Execute Detail Queries:
The function then iterates through the remaining queries in the queries array. Each detail query is executed using the headerId from the header query as a parameter. This ensures that the detail records are correctly related to the newly inserted header record.
5.
Commit or Rollback:
If all queries are executed successfully, the transaction is committed, making all changes permanent.
If any query fails, the transaction is rolled back, undoing all changes made during the transaction to maintain data integrity.
6.
Return Result:
The function returns the headerId, which represents the ID of the newly inserted header record. If an error occurs, the function returns -1 to indicate failure.

Example Usage:#

1.
Inserting a New Order with Items:

Important Considerations:#

Query Order:
Ensure that the first query in the queries array is the one that generates the identity ID, as this ID is used in the subsequent queries.
Parameter Usage:
Subsequent queries should use @headerId or a similar placeholder for the identity value. The function will replace this placeholder with the actual ID from the header query.
Error Handling:
The function includes error handling to roll back the transaction if any query fails. This ensures that partial data changes do not occur, maintaining data integrity.
Connection and Transaction Management:
The function ensures proper management of the database connection and transaction, including opening and closing the connection and committing or rolling back the transaction as needed.
By using _executeInsertQueriesInTransaction, you can handle related database operations in a single transaction, ensuring data consistency and integrity across multiple related records.
Previous
_executeInsertQuery
Next
_productsImport
Built with