ajDBUpdateRecord function
Description
The ajDBUpdateRecord function updates records in a database table satisfying the criteria specified in filter_condition. Please take note that to run this function from Excel, you would need to set up the Data Source Connection in ##ExternalResources.
Syntax
ajDBUpdateRecord( table_name, column_headers, data, [filter_type], [filter_condition], [table_schema], [data_source_id], [run_condition], [run_by_function_point_only] )
Argument Name | Argument Type | Description |
---|---|---|
table_name (required) | String | The name of the table to be updated. |
column_headers (required) | Range / Array | The columns to be updated. It could be a range of single row or column where each cell is a table column name or a data name defined in DB Schema. CLOB and BLOB are not supported. |
data (required) | Range / Array | The range where data is located. The orientation can either be landscape or portrait. The orientation should be the same as column_headers. |
filter_type (optional) | Double | The type of filtering. 3 types are supported 0, 1 and 2. You need to either provide filter_type+filter_condition or the DBSchema to call this function. Otherwise, an error will be raised. |
filter_condition (optional) | Range / Array | The range that defines the filter condition. filter_type = 0. The range defines the WHERE clause of a SQL statement. The range will be concatenated into a single string. For example, Name = 'peter' and class = 'B'. filter_type = 1. The range defines the filter condition in the Kendo grid style. Click Insert Snippet\Filter Condition (Filter Type 1) to add the preset required format. filter_type =2. The range defines a filter condition similar to the format used in MS Query. Click Insert Snippet\Filter Condition (Filter Type 2) to add the preset required format. Refer to the Filter Condition snippet to see how you can use this snippet to define the required fields for filter type 1, 2. |
table_schema (optional) | Range / Array | The range that defines the DB Schema. You need to either provide the filter_type+filter_condition or the DBSchema to call this function. When DBSchema is provided, the key column(s) must be included in the Column_headers and Data. Otherwise, an error will be raised. |
data_source_id (optional) | String | The data source shall be used in this database operation. It shall be defined in ##ExternalResources worksheet. The default value is "primary". |
run_condition (optional) | Boolean | The function will run when the value is TRUE. Otherwise, it will not run. The default value is TRUE. |
run_by_function_point_only (optional) | Boolean | If it equals FALSE, the function can be executed through ‘Excel Calculation’ (can be either Automatic or Manual, Calculate Now or Calculate Sheet) or Preview Function Point. If it equals TRUE, the function can be executed with Preview Function Point (AlchemyJ ribbon \ Preview Function Point) only. The default value is TRUE. |
The function will return:
1) Return Value: Number of affected records.
2) Return Type: Single Value
Example
We will use the following table in our examples. The table name is tb_customer. It has 5 columns and 5 rows.
Example 1
Update one column of records in a table in which the Customer Level equals 5. It updates the CUSTOMER_LVL column of matched records to 99 and 1 record affected.
Example 2
This example uses filter type 1 to update records where customer level equals 1 or 2.
The filter condition table was created via AlchemyJ ribbon Insert Snippet and select Filter Condition (Filter Type 1) .
Example 3
This example using filter type 2 to update records where gender equals F and customer level greater than 2.
The filter condition table was created via AlchemyJ ribbon Insert Snippet and select Filter Condition (Filter Type 2) .
Example 4
This example using DB Schema update records where name equals CHAN TAI MAN. The key column must be defined for filtering criteria. It can be defined in column headers or use the filter condition as Example 3.
Click here to download the use case workbooks for further reference.
Error Scenarios
It will return #VALUE! when missing any required parameter or mismatch parameter type. Besides, system will raise error for below scenario(s).
Error Scenario |
---|
And/Or in filter condition is invalid. |
Column header is not a single row range or a single column range. |
Content in filter condition range does not meet the expected format of specified filter type. |
Data name in filter condition does not exist in the provided table schema. |
Data name in filter condition is empty. |
Either filter condition or table schema must be provided. |
Invalid filter type, it must be 0, 1, or 2. |
Invalid operator value in filter condition. |
No Key order value defined in DB Schema. |
Number of columns in column_headers does not match with the number of data columns. |
Operator in filter condition is empty. |
Table name does not exist in provided DB Schema. |
Table name is empty. |
The primary key value is empty. |
The provided column header cannot be found in the specified table schema range. |
The table schema columns are invalid. |
The table schema range does not include a header or a row of data. |
Total number of filter condition columns is not even number. |