> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to Microsoft SQL Server

> How to connect Atoti DirectQuery to Microsoft SQL Server, covering credentials via `SQLServerDataSource`, SQL login configuration, and the `CONNECT` and `SELECT` permissions required on target schemas or tables

## Credentials

Atoti DirectQuery delegates the credentials handling to a Microsoft
[`SQLServerDataSource`](https://javadoc.io/doc/com.microsoft.sqlserver/mssql-jdbc/9.4.1.jre11/com.microsoft.sqlserver.jdbc/com/microsoft/sqlserver/jdbc/SQLServerDataSource.html).

Executing SQL in Microsoft SQL Server requires a
[SQL user](https://docs.microsoft.com/en-us/sql/t-sql/statements/create-user-transact-sql?view=sql-server-ver16)
in the target database. As described in the Microsoft documentation, several authentication methods can be used.

The following example builds the data source with an SQL login:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
final SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setUser(username);
dataSource.setPassword(password);
// Set up the database to connect to
dataSource.setPortNumber(port);
dataSource.setDatabaseName(DATABASE_NAME);
// Setup connection security parameters
dataSource.setEncrypt("true");
dataSource.setTrustServerCertificate(true);
dataSource.setSendTimeAsDatetime(false);
// Setup number of connection attempts (first tries may fail when pool needs to warm up)
dataSource.setConnectRetryCount(CONNECT_RETRY_ATTEMPTS);
dataSource.setConnectRetryInterval(CONNECT_RETRY_INTERVAL);
final MsSqlClientSettings clientSettings =
    MsSqlClientSettings.builder().dataSource(dataSource).build();
```

> [!NOTE]
>
> More examples are available in the Microsoft documentation:
>
> * [Data source sample](https://docs.microsoft.com/en-us/sql/connect/jdbc/data-source-sample?view=sql-server-ver16)

## Permissions

The SQL user used by DirectQuery needs to have the permissions to run queries in Microsoft SQL Server.

To be able to run queries, the `CONNECT` permission is needed on the Microsoft SQL Server to be used,
then the `SELECT` permission is needed on the schemas or tables the user will use.
