intermediate5 min read

SQL Server

Connecting SQL Server

AI for Database supports Microsoft SQL Server 2017+, Azure SQL Database, and Amazon RDS for SQL Server.

Create a Read-Only User

sql
-- Create a login
CREATE LOGIN aifordb_reader WITH PASSWORD = 'a-strong-random-password';

-- Switch to your database
USE your_database;

-- Create a user mapped to the login
CREATE USER aifordb_reader FOR LOGIN aifordb_reader;

-- Grant read access
ALTER ROLE db_datareader ADD MEMBER aifordb_reader;

The db_datareader role grants SELECT access on all tables and views in the database.

Connection Settings

FieldExample
Hostsqlserver.example.com
Port1433
Databaseproduction
Usernameaifordb_reader
Password(your password)
Encrypttrue

Azure SQL

For Azure SQL Database, use the full server name (e.g., yourserver.database.windows.net). Ensure the AI for Database IP is added to the Azure SQL firewall rules under Networking.

Azure Active Directory authentication is supported on Enterprise plans. Contact support for setup instructions.

Windows Authentication

For on-premise SQL Server instances using Windows Authentication, you will need to use the self-hosted version of AI for Database and configure the service to run under a Windows account that has read access to the database.

Troubleshooting

Cannot open server requested by the login: Ensure the user is mapped to the correct database. Run SELECT name FROM sys.database_principals WHERE name = 'aifordb_reader'; to check.

Encryption not supported: Set the Encrypt field to false if your SQL Server does not have TLS configured. We recommend enabling TLS for production.