> Every UQL docs page, as Markdown: https://uql-orm.dev/llms.txt
> The same docs over MCP: https://uql-orm.dev/mcp
> Before writing UQL code, read the skill: https://uql-orm.dev/.well-known/agent-skills/uql-orm/SKILL.md

# Microsoft SQL Server

> Run UQL on SQL Server 2017 and up with the mssql driver, and what behaves differently there.

Source: https://uql-orm.dev/mssql

UQL runs on SQL Server 2017 and up through the pure-JavaScript [`mssql`](https://www.npmjs.com/package/mssql) driver.

## Install and connect

```sh
npm install uql-orm mssql
```

```ts
import { MsSqlQuerierPool } from 'uql-orm/mssql';

export const pool = new MsSqlQuerierPool({
  server: 'localhost',
  database: 'app',
  user: 'app',
  password: process.env.DB_PASSWORD,
  options: { encrypt: true, trustServerCertificate: false },
});
```

The pool takes `mssql`’s own `config`. See [Pool](https://uql-orm.dev/pool.md) for sizing and shutdown.

> **Turn on snapshot isolation**
>
> SQL Server’s default `READ COMMITTED` takes shared read locks, so concurrent ORM work deadlocks where other engines would not. Run this once per database:
>
> ```sql title="SQL Server"
> ALTER DATABASE app SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
> ```

## What differs

Strings are always `NVARCHAR`, and comparisons follow the database collation, case-insensitive by default. `$regex` is case-sensitive and needs SQL Server 2025 at compatibility level 170 (`REGEXP_LIKE`). `$text` throws, since `CONTAINS` needs a full-text catalog UQL does not create.

Vector search needs SQL Server 2025 and a declared `dimensions`. It is exact, computing every distance with `VECTOR_DISTANCE`, because the DiskANN index is still in preview.

`OUTPUT INSERTED` returns one id per row, so `insertMany` ids are exact. Writing an explicit value into an identity column works too; UQL wraps that insert in `SET IDENTITY_INSERT`.

The server refuses two cascade paths to one table (error 1785) and has no `RESTRICT`; the default `NO ACTION` avoids both. The `uuidv7()` SQL value throws, so generate v7 ids in code with `onInsert`. [Streaming](https://uql-orm.dev/querying/streaming.md) uses the driver’s own row stream.

## Migrations

[Migrations](https://uql-orm.dev/migrations.md) diff the schema like on any other engine. SQL Server names column defaults, `CHECK` and `UNIQUE` constraints itself, so UQL drops them before dropping a column and restores the default after a retype. Renames use `sp_rename`, and creates are guarded by catalog lookups in place of `IF NOT EXISTS`.
