使用 SQL 批量合并
将数据源中的数据批量合并到数据库表中。
命令可用性: 本地 IBM RPA SaaS 和 IBM RPA
Description
此命令运行合并操作,在此操作中,它将插入新数据并将现有数据从数据源批量更新到数据库表。
脚本语法
IBM RPA 的专有脚本语言的语法与其他编程语言类似。 该脚本语法在脚本文件中定义命令的语法。 您可以在 IBM RPA Studio的 脚本 方式下使用此语法。
sqlBulkMerge --dataTable(DataTable) --connection(DbConnection) --tablename(String) [--batchsize(Numeric)] [--mappings(String)] [--timeout(TimeSpan)] (Boolean)=value
依赖关系
您必须创建具有有效连接字符串的数据库连接。 根据您使用的数据库管理系统 (DBMS),使用以下命令:
注:此命令不支持 ODBC 连接。
输入参数
下表显示了此命令中提供的输入参数的列表。 在表中,您可以看到在 IBM RPA Studio的脚本方式及其 Designer 方式等效标签中工作时的参数名称。
| 设计器方式标签 | 脚本方式名称 | 必需的 | 接受的变量类型 | Description |
|---|---|---|---|---|
| 数据表 | dataTable |
Required |
Data Table |
作为数据源的数据表。 |
| 连接 | connection |
Required |
Database Connection |
数据库连接变量。 |
| 表名 | tablename |
Required |
Text |
数据库中表的名称。 该表必须存在于数据库中。 |
| 批处理大小 | batchsize |
Optional |
Number |
批次大小。 根据您提供的大小将批量插入分割成多个批次。 无论批处理大小的值如何,都将插入所有行。 |
| 映射 | mappings |
Optional |
Text |
从数据源表列到数据库表列的映射。 您必须填写以下字段: 列名:要映射的数据表列名称。 列号:要映射的数据表列索引。 列索引从 1. 开始。未命名的 字段:数据库表列名称。 要识别数据表列,您必须在列名或列号字段中输入值。 |
| 超时 | timeout |
Optional |
Time Span, Number, Text |
等待运行命令的最长时间。 缺省超时为 5 秒 (5000 或 00:00:05)。请参阅 timeout 参数 部分以获取详细信息。 |
timeout 参数
您可以通过输入 String 表示法 (以毫秒计) 或 Time span 表示法来定义超时。 例如:
String表示:50000(50 秒)Time span表示:2.05:20:10.001(两天,五小时,二十分钟,十秒和一毫秒)
对于这些表示, IBM RPA Studio 会将它们舍入到最接近的秒数值。 例如:
1001毫秒为00:00:01.001,向上舍入到00:00:0260020毫秒为00:01:00.020,向上舍入到00:01:01
输出参数
| 设计器方式标签 | 脚本方式名称 | 接受的变量类型 | Description |
|---|---|---|---|
| 成功 | value |
Boolean |
如果命令成功运行,那么返回 True,否则返回 False。 |
示例
以下代码示例演示了如何通过将数据表用作数据源来批量合并数据库表中的数据。 脚本需要“我的文档”文件夹中的两个文件:sample.db 数据库文件和 sample.csv 文件。 sample.csv 文件存储了您需要与数据库表中的数据合并的样本数据。 它会将数据与数据库表上的 identifier、sample 和 type 列进行合并。
defVar --name pathMyDocuments --type String
defVar --name databaseFile --type String
defVar --name dataTableFile --type String
defVar --name dataSource --type DataTable
defVar --name dbConnection --type DbConnection
// Gets the 'My Documents' folder path.
getSpecialFolder --folder "MyDocuments" pathMyDocuments=value
// Assign the 'sample.db' folder path to the 'databaseFile' variable.
setVar --name "${databaseFile}" --value "${pathMyDocuments}\\sample.db"
// Assign the 'sample.csv' folder path to the 'dataTableFile' variable.
setVar --name "${dataTableFile}" --value "${pathMyDocuments}\\sample.csv"
// Reads a CSV file and gets the sample data table to use as data source. The table is stored in the cache to be handled.
readCSV --filepath "${dataTableFile}" --delimiter "," --hasheaders --encoding "Default" --missingfieldaction "ParseError" dataSource=value
// Connects to the database management system (DBMS). You must provide a valid connection string to connect with the database before you run this example.
sqliteConnect --connectionString "Data Source=${databaseFile};Version=3;UseUTF16Encoding=True;" dbConnection=connection
// Merges a data in the data source to the database table.
sqlBulkMerge --dataTable ${dataSource} --connection ${dbConnection} --tablename sample --mappings "number=1=identifier,number=2=sample,number=3=type" --timeout "00:00:52"