Prerequisites
Before proceeding, ensure the following prerequisites are met:- Install MindsDB locally via Docker or use MindsDB Cloud.
- To connect Microsoft SQL Server to MindsDB, install the required dependencies following this instruction.
Installation
The MSSQL handler supports two connection methods:Option 1: Standard Connection (pymssql - Recommended)
pymssql, which provides native FreeTDS-based connections. Works on all platforms.
Option 2: ODBC Connection (pyodbc)
pymssql and pyodbc for ODBC driver support.
Additional requirements for ODBC:
- System ODBC libraries: On Linux, install
unixodbcandunixodbc-dev - Microsoft ODBC Driver for SQL Server:
- Linux:
- macOS:
brew install msodbcsql18 - Windows: Download from Microsoft
- Linux:
Connection
Establish a connection to your Microsoft SQL Server database from MindsDB by executing the following SQL command:user: The username for the Microsoft SQL Server.password: The password for the Microsoft SQL Server.hostThe hostname, IP address, or URL of the Microsoft SQL Server.databaseThe name of the Microsoft SQL Server database to connect to.
port: The port number for connecting to the Microsoft SQL Server. Default is 1433.server: The server name to connect to. Typically only used with named instances or Azure SQL Database.
ODBC Connection
The handler also supports ODBC connections viapyodbc for advanced scenarios like Windows Authentication or specific driver requirements.
Setup
- Install:
pip install mindsdb[mssql-odbc] - Install system ODBC driver (see Installation section above)
driver: The ODBC driver name (e.g., “ODBC Driver 18 for SQL Server”). When specified, enables ODBC mode.use_odbc: Set totrueto explicitly use ODBC. Optional ifdriveris specified. If this is true default driver is set asODBC Driver 17 for SQL Server.encrypt: Connection encryption:"yes"or"no". Driver 18 defaults to"yes".trust_server_certificate: Whether to trust self-signed certificates:"yes"or"no".connection_string_args: Additional connection string arguments.
Example: Azure SQL Database with Encryption:
Example: Local Development (Self-Signed Certificate):
Usage
Retrieve data from a specified table by providing the integration name, schema, and table name:The above examples utilize
mssql_datasource as the datasource name, which is defined in the CREATE DATABASE command.Performance Optimization for Large Datasets
The handler is optimized for efficient data processing, but for very large result sets (millions of rows):- Use SQL Server’s filtering: Apply
WHEREclauses to filter data on the server side - Use pagination: Use
TOP/OFFSET-FETCHin SQL Server orLIMITin MindsDB queries - Aggregate when possible: Use
GROUP BY,COUNT(),AVG(), etc. to reduce data volume - Index your tables: Ensure proper indexes on SQL Server for query performance
Troubleshooting Guide
Database Connection Error- Symptoms: Failure to connect MindsDB with the Microsoft SQL Server database.
- Checklist:
- Make sure the Microsoft SQL Server is active.
- Confirm that host, port, user, and password are correct. Try a direct Microsoft SQL Server connection using a client like SQL Server Management Studio or DBeaver.
- Ensure a stable network between MindsDB and Microsoft SQL Server.
SQL statement cannot be parsed by mindsdb_sql- Symptoms: SQL queries failing or not recognizing table names containing spaces or special characters.
- Checklist:
- Ensure table names with spaces or special characters are enclosed in backticks.
- Examples:
- Incorrect: SELECT * FROM integration.travel data
- Incorrect: SELECT * FROM integration.‘travel data’
- Correct: SELECT * FROM integration.`travel data`
ODBC Driver Connection Error- Symptoms: Errors like “Driver not found”, “Can’t open lib ‘ODBC Driver 17 for SQL Server’”, or “pyodbc is not installed”.
- Checklist:
- Verify pyodbc is installed:
pip list | grep pyodbc - Check system ODBC libraries:
ldconfig -p | grep odbc(Linux) should show libodbc.so - Verify ODBC drivers: Run
odbcinst -q -dto list installed drivers - Match driver name exactly: Use the exact name from
odbcinst -q -d(case-sensitive) - For Driver 18 encryption errors: Add
"encrypt": "yes", "trust_server_certificate": "yes"for local/dev servers - Test connection manually:
- Verify pyodbc is installed: