Building an AI Chatbot for your SaaS app in 1 dayLivestream March 11

Skip to main content

query_rw

Execute SQL queries that can modify data or schema in MotherDuck.

Description

The query_rw tool executes SQL against your MotherDuck databases, including operations that change data or schema. For cross-database queries, use fully qualified names: database.schema.table (or database.table for the main schema).

Input Parameters

ParameterTypeRequiredDescription
databasestringNoDatabase context for the query. Required when the statement targets database objects. Optional for account-level operations.
sqlstringYesDuckDB SQL statement to execute

Output Schema

Same as query:

{
"success": boolean,
"columns": string[], // Column names (on success)
"columnTypes": string[], // Column types (on success)
"rows": any[][], // Query results (on success)
"rowCount": number, // Number of rows returned (on success)
"error": string, // Error message (on failure)
"errorType": string // Error type (on failure)
}

Limits

  • Result limit: Maximum 2,048 rows and 50,000 characters. Results exceeding these limits will be truncated with a truncation message.
  • Query timeout: 55 seconds. Queries exceeding this limit will be cancelled server-side and the tool will respond with an error message.

Example Usage

Insert rows:

Insert a new customer 'Acme Corp' with id 100 into my_database.customers
{
"database": "my_database",
"sql": "INSERT INTO customers (id, name) VALUES (100, 'Acme Corp')"
}

Update and delete:

In my_database, set status to 'shipped' for all orders in the orders table where status is 'pending', then delete the old log entries from audit_log

The AI assistant can call query_rw with the appropriate UPDATE and DELETE statements (or multiple calls if the client requires one statement per call).

Create table:

Create a table my_database.main.events with columns id (BIGINT), name (VARCHAR), created_at (TIMESTAMP)
{
"database": "my_database",
"sql": "CREATE TABLE main.events (id BIGINT, name VARCHAR, created_at TIMESTAMP)"
}

Account-level operations (database optional):

Create a new database called reporting
{
"sql": "CREATE DATABASE reporting"
}

For account-level operations, omit database and pass only sql.