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

_executeInsertQuery

Usage Documentation: _executeInsertQuery JavaScript Function#

The _executeInsertQuery function allows you to execute an SQL INSERT query against a SQL Server database from within your JavaScript code. It returns the ID of the newly inserted record, which is typically generated by an IDENTITY column in the database.

Function Signature:#

Parameters:#

query (string):
Description: The SQL INSERT statement that you want to execute. This should be a complete SQL INSERT command, including the table name, columns, and values.
Type: string
Example:

Return Value:#

Returns:
The ID of the newly inserted record. This ID is retrieved from the IDENTITY column of the inserted record, if applicable.
Type: number
Example:

Example Usage:#

Basic Insert Operation:

Detailed Explanation:#

1.
SQL INSERT Query:
The query parameter should be a complete SQL INSERT statement. Ensure that the statement correctly specifies the table and columns where data will be inserted.
Example of a valid query:
2.
Executing the Query:
The function executes the provided INSERT query against the SQL Server database.
After execution, the function retrieves the last generated identity value using SCOPE_IDENTITY(), which returns the ID of the newly inserted record in the same scope.
3.
Returning the New ID:
The function returns the ID of the newly inserted record. This is useful when you need to reference or use the ID of the newly inserted row in subsequent operations.

Important Considerations:#

SQL Injection:
Ensure that the query parameter is sanitized and does not include user inputs directly to avoid SQL injection attacks. Use parameterized queries where possible.
Error Handling:
If the query is invalid or if there is an issue with the database connection, the function may throw an exception or return an error. Make sure to handle errors appropriately in your JavaScript code.
Identity Column:
The function assumes that the table includes an IDENTITY column or a similar mechanism for auto-generating unique IDs. If the table does not have such a column, the function may not return the correct value.
This function is a convenient way to insert data into a SQL Server database and obtain the ID of the newly created record, making it useful for scenarios where you need to track or reference newly inserted rows.
Previous
_executeStoreProcedure
Next
_executeInsertQueriesInTransaction
Built with