update_row
Modify existing database records by specifying the table, primary key, and new column values to update.
Instructions
Update an existing row by primary key.
Args:
table: Table name (can include schema: 'dbo.Users' or 'Users')
id: Primary key value of the row to update
data: Dictionary of column names and new values
Returns:
Dictionary with:
- status: 'success' or error
- table: Full table name
- updated: The updated row
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table | Yes | ||
| id | Yes | ||
| data | Yes |
Implementation Reference
- src/mssql_mcp/tools/crud.py:188-252 (handler)The handler function for the update_row tool. Decorated with @mcp.tool() for registration in the MCP server. Performs SQL UPDATE on the table row identified by primary key, using OUTPUT to retrieve the updated row. Includes safety checks for read-only mode and primary key existence.@mcp.tool() def update_row(table: str, id: Any, data: dict[str, Any]) -> dict[str, Any]: """Update an existing row by primary key. Args: table: Table name (can include schema: 'dbo.Users' or 'Users') id: Primary key value of the row to update data: Dictionary of column names and new values Returns: Dictionary with: - status: 'success' or error - table: Full table name - updated: The updated row """ try: manager = get_connection_manager() config = manager.config # Check read-only mode if config.read_only: return {"error": "Write operations disabled in read-only mode"} schema, table_name = parse_table_name(table) if not data: return {"error": "No data provided for update"} # Get primary key column pk_cols = _get_primary_key_columns(schema, table_name) if not pk_cols: return {"error": f"No primary key found for table {schema}.{table_name}"} # Build UPDATE statement with OUTPUT clause set_clauses = ", ".join([f"[{k}] = %s" for k in data]) params = tuple(data.values()) + (id,) query = f""" UPDATE [{schema}].[{table_name}] SET {set_clauses} OUTPUT INSERTED.* WHERE [{pk_cols[0]}] = %s """ rows = manager.execute_query(query, params) if not rows: return { "error": f"No row found with {pk_cols[0]} = {id}", "table": f"{schema}.{table_name}", } return { "status": "success", "table": f"{schema}.{table_name}", "updated": rows[0], } except QueryError as e: logger.error(f"Error updating row in {table}: {e}") return {"error": str(e)} except Exception as e: logger.error(f"Unexpected error updating row in {table}: {e}") return {"error": str(e)}