執行 SQL 指令

針對與資料庫的連線執行 SQL 陳述式,並傳回受影響的列數。

指令可用性: IBM RPA SaaS 及 IBM RPA 內部部署

Script 語法

IBM RPA 的專有 Script 語言具有類似於其他程式設計語言的語法。 Script 語法在 Script 檔中定義指令的語法。 您可以在 IBM RPA Studio的 Script 模式中使用此語法。

sqlExecute --connection(DbConnection) --statement(String) [--timeout(TimeSpan)] (Numeric)=value

相依關係

您必須使用有效的連線字串來建立資料庫連線。 根據您使用的資料庫管理系統 (DBMS),使用下列指令:

輸入參數

下表顯示此指令中可用的輸入參數清單。 在表格中,當您在 IBM RPA Studio的 Script 模式及其 Designer 模式對等標籤中工作時,可以看到參數名稱。

設計程式模式標籤 Script 模式名稱 必要 接受的變數類型 說明
連線 connection Required Database Connection 資料庫連線變數。
SQL 陳述式 statement Required Text SQL 陳述式。

如需詳細資料,請參閱 statement 參數 一節。
逾時值 timeout Optional Time Span, Number, Text 等待執行指令的時間上限。 預設逾時為 5 秒 (500000:00:05)。

如需詳細資料,請參閱 timeout 參數 一節。

statement 參數

根據您所使用 DBMS 中的 SQL 語法來撰寫陳述式。 您可能可以使用 CREATE TABLEDROP TABLEINSERTUPDATEDELETE 陳述式來執行查詢。

在 Oracle™ 資料庫中,請勿關閉具有分號 (;) 的 SQL 陳述式,因為它可能會引發異常狀況。 此外,每個查詢只能執行一個陳述式。

timeout 參數

您可以輸入 String 表示法 (毫秒) 或 Time span 表示法來定義逾時。 例如:

  • String 表示法: 50000 (50 秒)
  • Time span 表示法: 2.05:20:10.001 (2 天、5 小時、20 分鐘、10 秒及 1 毫秒)

對於這些表示法, IBM RPA Studio 會將它們四捨五入到最接近的秒數值。 例如:

  • 1001 毫秒為 00:00:01.001 ,並四捨五入至 00:00:02
  • 60020 毫秒為 00:01:00.020 ,並四捨五入至 00:01:01

如果您將逾時值設為零,則下列資料庫系統會將逾時值設為無限制,這表示指令會無限期等待執行:

  • 透過 ODBC 連接的資料庫
  • Oracle™
  • SQL Server®
  • SQLite®

部分資料庫系統使用逾時來預估執行查詢所需的時間。 對於逾時限制異常狀況,您可以將逾時設為零,或新增更大的逾時。

輸出參數

設計程式模式標籤 Script 模式名稱 接受的變數類型 說明
value Number 傳回受影響的列數。

範例

下列程式碼範例示範如何在資料庫中執行 CREATE TABLEINSERTUPDATE 查詢。 請考量稱為 sample.db的空資料庫檔案。 在此範例中,除了 CREATE TABLE 查詢之外,在執行 INSERTUPDATE 查詢之後,您會收到受這些陳述式影響的列數。 請注意,您也可以透過此指令一次性執行這些陳述式。

defVar --name dbConnection --type DbConnection
defVar --name affectedRows --type Numeric
defVar --name databaseFile --type String
defVar --name pathMyDocuments --type String
// Gets the My Documents folder path.
getSpecialFolder --folder "MyDocuments" pathMyDocuments=value
// Assigns the 'sample.db' folder path to the 'databaseFile' variable.
setVar --name "${databaseFile}" --value "${pathMyDocuments}\\sample.db"
// 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
// Runs the CREATE TABLE query.
sqlExecute --connection ${dbConnection} --statement "CREATE TABLE sample_table(\r\n   sample_column TEXT,\r\n   identifier INTEGER,\r\n PRIMARY KEY(\"identifier\" AUTOINCREMENT)\r\n);" --timeout "00:00:05.2000000"
// Runs the INSERT query.
sqlExecute --connection ${dbConnection} --statement "INSERT INTO sample_table (sample_column) VALUES (\'First text value\');\r\nINSERT INTO sample_table ( sample_column) VALUES (\'Second text value\');" --timeout "00:00:05.2000000" affectedRows=value
// Logs the number of rows affected by the INSERT query.
logMessage --message "Inserting value: ${affectedRows}" --type "Info"
// Runs the UPDATE query.
sqlExecute --connection ${dbConnection} --statement "UPDATE sample_table\r\nSET sample_column = \'Changing the first text value\'\r\nWHERE sample_column = \'First text value\';" --timeout "5.18:53:20" affectedRows=value
// Logs the number of rows affected by the UPDATE query.
logMessage --message "Updating value: ${affectedRows}" --type "Info"
// Ends the connection with the database.
sqlDisconnect --connection ${dbConnection}