docs/integrations/data-integrations/microsoft-sql-server.mdx
This documentation describes the integration of MindsDB with Microsoft SQL Server, a relational database management system developed by Microsoft. The integration allows for advanced SQL functionalities, extending Microsoft SQL Server's capabilities with MindsDB's features.
Before proceeding, ensure the following prerequisites are met:
The MSSQL handler supports two connection methods:
pip install mindsdb[mssql]
This installs pymssql, which provides native FreeTDS-based connections. Works on all platforms.
pip install mindsdb[mssql-odbc]
This installs both pymssql and pyodbc for ODBC driver support.
Additional requirements for ODBC:
unixodbc and unixodbc-dev
sudo apt-get install unixodbc unixodbc-dev
# Add Microsoft repository
curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
# Install ODBC Driver 18
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
brew install msodbcsql18To verify installed drivers:
odbcinst -q -d
Establish a connection to your Microsoft SQL Server database from MindsDB by executing the following SQL command:
CREATE DATABASE mssql_datasource
WITH ENGINE = 'mssql',
PARAMETERS = {
"host": "127.0.0.1",
"port": 1433,
"user": "sa",
"password": "password",
"database": "master"
};
Required connection parameters include the following:
user: The username for the Microsoft SQL Server.password: The password for the Microsoft SQL Server.host The hostname, IP address, or URL of the Microsoft SQL Server.database The name of the Microsoft SQL Server database to connect to.Optional connection parameters include the following:
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.The handler also supports ODBC connections via pyodbc for advanced scenarios like Windows Authentication or specific driver requirements.
pip install mindsdb[mssql-odbc]Basic ODBC Connection:
CREATE DATABASE mssql_odbc_datasource
WITH ENGINE = 'mssql',
PARAMETERS = {
"host": "127.0.0.1",
"port": 1433,
"user": "sa",
"password": "password",
"database": "master",
"driver": "ODBC Driver 18 for SQL Server" -- Specifying driver enables ODBC
};
ODBC-specific Parameters:
driver: The ODBC driver name (e.g., "ODBC Driver 18 for SQL Server"). When specified, enables ODBC mode.use_odbc: Set to true to explicitly use ODBC. Optional if driver is specified. If this is true default driver is set as ODBC 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.CREATE DATABASE azure_sql_datasource
WITH ENGINE = 'mssql',
PARAMETERS = {
"host": "myserver.database.windows.net",
"port": 1433,
"user": "adminuser",
"password": "SecurePass123!",
"database": "mydb",
"driver": "ODBC Driver 18 for SQL Server",
"encrypt": "yes",
"trust_server_certificate": "no"
};
CREATE DATABASE local_mssql
WITH ENGINE = 'mssql',
PARAMETERS = {
"host": "localhost",
"port": 1433,
"user": "sa",
"password": "YourStrong@Passw0rd",
"database": "testdb",
"driver": "ODBC Driver 18 for SQL Server",
"encrypt": "yes",
"trust_server_certificate": "yes" -- Allow self-signed certs
};
Retrieve data from a specified table by providing the integration name, schema, and table name:
SELECT *
FROM mssql_datasource.schema_name.table_name
LIMIT 10;
Run T-SQL queries directly on the connected Microsoft SQL Server database:
SELECT * FROM mssql_datasource (
--Native Query Goes Here
SELECT
SUM(orderqty) total
FROM Product p JOIN SalesOrderDetail sd ON p.productid = sd.productid
JOIN SalesOrderHeader sh ON sd.salesorderid = sh.salesorderid
JOIN Customer c ON sh.customerid = c.customerid
WHERE (Name = 'Racing Socks, L') AND (companyname = 'Riding Cycles');
);
The handler is optimized for efficient data processing, but for very large result sets (millions of rows):
WHERE clauses to filter data on the server sideTOP/OFFSET-FETCH in SQL Server or LIMIT in MindsDB queriesGROUP BY, COUNT(), AVG(), etc. to reduce data volumeExample - Paginated Query:
SELECT * FROM mssql_datasource (
SELECT TOP 100000 *
FROM large_table
ORDER BY id
OFFSET 0 ROWS
);
pip list | grep pyodbcldconfig -p | grep odbc (Linux) should show libodbc.soodbcinst -q -d to list installed driversodbcinst -q -d (case-sensitive)"encrypt": "yes", "trust_server_certificate": "yes" for local/dev serversimport pyodbc
print(pyodbc.drivers()) # Should list available drivers