insert_row
Add new data records to SQL Server tables by specifying column values. This tool enables structured data insertion with automatic handling of identity columns and confirmation of successful operations.
Instructions
Insert a new row into a table.
Args:
table: Table name (can include schema: 'dbo.Users' or 'Users')
data: Dictionary of column names and values to insert
Returns:
Dictionary with:
- status: 'success' or error
- table: Full table name
- inserted: The inserted row (including generated identity columns)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table | Yes | ||
| data | Yes |
Implementation Reference
- src/mssql_mcp/tools/crud.py:131-185 (handler)The insert_row tool handler function, decorated with @mcp.tool() for automatic registration. It inserts a new row into the specified table using an SQL INSERT statement with OUTPUT clause to retrieve the inserted row data, including any generated identity columns. Handles errors like read-only mode and query failures.@mcp.tool() def insert_row(table: str, data: dict[str, Any]) -> dict[str, Any]: """Insert a new row into a table. Args: table: Table name (can include schema: 'dbo.Users' or 'Users') data: Dictionary of column names and values to insert Returns: Dictionary with: - status: 'success' or error - table: Full table name - inserted: The inserted row (including generated identity columns) """ 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 insert"} # Build INSERT statement with OUTPUT clause cols = ", ".join([f"[{k}]" for k in data]) placeholders = ", ".join(["%s"] * len(data)) values = tuple(data.values()) query = f""" INSERT INTO [{schema}].[{table_name}] ({cols}) OUTPUT INSERTED.* VALUES ({placeholders}) """ rows = manager.execute_query(query, values) # The OUTPUT clause returns the inserted row inserted = rows[0] if rows else data return { "status": "success", "table": f"{schema}.{table_name}", "inserted": inserted, } except QueryError as e: logger.error(f"Error inserting row into {table}: {e}") return {"error": str(e)} except Exception as e: logger.error(f"Unexpected error inserting row into {table}: {e}") return {"error": str(e)}