Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,150 of 9,150 pages
About the help in other languages
All help pages

Microsoft SQL Server driver

This page is for administrators and developers who want to run Exponential on Microsoft SQL Server, and for contributors who work on the driver. From Exponential 6.0.15 the kernel has a SQL Server driver of its own…

Download as PDF

Address: https://doc.exponential.earth/learninminutes/exphelp/viewer/features/6.0/sqlserver-database-driver.md

On this page

Microsoft SQL Server driver

This page is for administrators and developers who want to run Exponential on Microsoft SQL Server, and for contributors who work on the driver. From Exponential 6.0.15 the kernel has a SQL Server driver of its own, expSQLServerDB in lib/ezdb/classes/, written anew under the GPL (version 2 or later). It is not derived from the old xrowmssql extension; that extension was read for its problems only, and every one of them is now a test.

Status. The driver, its schema handler expSQLServerSchema, the kernel schema in kernel/sql/sqlserver/kernel_schema.sql, the portable SQL helpers of every driver, the installer entries and the DFS cluster backend (Clustering on SQL Server) are done. A site installs on SQL Server with install.php --db=sqlserver (below): measured on SQL Server 2022, 16 steps, 0 errors, 323 content objects.

Contents

  1. Supported servers and PHP layers
  2. Settings
  3. What the driver does differently
  4. Helpers every driver offers
  5. Installing a site on SQL Server
  6. Read replicas and failover
  7. Tested toolchain (W0)
  8. Tests
  9. Traceability: the old extension's problems

Supported servers and PHP layers

ServerSupport
SQL Server 2017 (14.x)supported; the floor (STRING_AGG, TRIM, CONCAT_WS). md5() matches PHP only for ASCII text
SQL Server 2019 (15.x)supported; UTF-8 collations
SQL Server 2022 (16.x)supported; the development and main CI target; Encrypt=strict (TDS 8.0)
SQL Server 2025 (17.x)supported; in the CI matrix
Azure SQL Database, Azure SQL Managed Instancesupported (engine editions 5 and 8); transient errors are retried
Azure SQL Edgenot supported: Microsoft retired it on 30 September 2025. Use SQL Server 2022 or later on Linux
SQL Server 2016 and olderthe driver connects and warns; not supported

databaseName() is exactly SQLServer. PHP layers, chosen with Layer=auto in this order:

LayerExtensionNotes
sqlsrvMicrosoft's sqlsrv (PECL)Windows and Linux (over ODBC Driver 17/18). Recommended
pdo_sqlsrvMicrosoft's pdo_sqlsrv (PECL)the driver switches MARS off (with MARS a BEGIN TRANSACTION batch is rolled back at its end) and uses direct execution
pdo_odbcPHP's PDO_ODBC + an ODBC driver for SQL Serverworks with ODBC Driver 18 out of the box (Plesk PHP has it). Statement text travels in the client code page, so text is always bound; batches without rows run through exec()
pdo_dblibPHP's pdo_dblib over FreeTDSfallback; TDS 7.4, UTF-8. No MultiSubnetFailover, no ApplicationIntent

Settings

[DatabaseSettings]
DatabaseImplementation=sqlserver
Server=sql1.example.com
Port=1433
User=exponential
Password=...
Database=site

ImplementationAlias[sqlserver], [mssql] and [sqlsrv] all name expSQLServerDB. The [SQLServerSettings] of site.ini (all with defaults): Layer, ODBCDriver, Encrypt (yes, no, strict), TrustServerCertificate, MultiSubnetFailover, ApplicationIntent, ReplicaApplicationIntent, LoginTimeout, ConnectRetryCount (0: ODBC's own reconnect is off, the driver reconnects itself), ConnectionPooling (disabled: see below), LockTimeout (milliseconds, -1 waits), LiteralMode (bind, prefix), DeadlockRetries, DeadlockRetryWait, ReconnectOnLoss, ApplicationName, InListChunkSize, DatabaseCollation.

Server side. Create the database with ALTER DATABASE [site] SET READ_COMMITTED_SNAPSHOT ON: readers then never wait for writers, which is what the kernel expects from MySQL and PostgreSQL. The driver notes it in the debug output when it is off. A UTF-8 collation (Latin1_General_100_CI_AS_SC_UTF8, 2019 and later) is recommended; text columns should be NVARCHAR.

What the driver does differently

  • Paging on the server: ORDER BY ... OFFSET n ROWS FETCH NEXT m ROWS ONLY. A statement without an outer ORDER BY gets ORDER BY (SELECT NULL); one with TOP, its own OFFSET, or DISTINCT/UNION without ORDER BY is wrapped. An ORDER BY inside a subquery, OVER(), a literal or a comment does not count (all found by a lexer). limit 0 sends no statement (FETCH NEXT 0 is error 10744).
  • New ids through SCOPE_IDENTITY() in the INSERT's own batch: not @@IDENTITY (a trigger's id), and not a later batch (a bound insert runs in its own scope). NULL or 0 for the identity column leaves the column out; a real number switches IDENTITY_INSERT on for that statement only, and off again even when it fails.
  • Text as bound NVARCHAR parameters for queries and data changes, as N'...' literals in DDL, GROUP BY statements and statements with more than 2,000 literals (the server takes 2,100 parameters). A backslash before a line break, which T-SQL removes, is kept. MySQL backticks become brackets.
  • Session: NOCOUNT, XACT_ABORT (an error rolls the whole transaction back), the ANSI options, DATEFORMAT ymd, LOCK_TIMEOUT, READ COMMITTED.
  • Errors: deadlock (1205), lock timeout (1222), duplicate key (2627/2601), FK (547), NOT NULL (515), truncation (2628/8152), lost connections and Azure transient errors are told apart. A single statement outside a transaction is sent again after a deadlock; a lost connection outside a transaction is opened again (persistent Velocity workers survive a server restart); inside a transaction the transaction is invalid. retryOnDeadlock() runs a whole transaction again.
  • Locks: lock() takes TABLOCKX, HOLDLOCK in a transaction; getAppLock() / releaseAppLock() give named session locks (sp_getapplock).
  • Temporary tables: CREATE TEMPORARY TABLE x becomes #x; the driver drops those left at the next request reset.
  • IN lists in chunks joined with OR; an empty list is (1=0).
  • Catalog from sys.*: tables, sequences, views, triggers, indexes; tableColumns() for the schema read-back.

Helpers every driver offers

Kernel SQL that is MySQL-only today becomes portable through driver helpers, never through per-engine branches: sqlLimit(), sqlUpsert() (here MERGE ... WITH (HOLDLOCK)), sqlGroupConcat() (STRING_AGG), sqlUnixTimestamp() / sqlFromUnixTimestamp() (DATEDIFF_BIG, safe past 2038), sqlQuoteIdentifier() and sqlForUpdate() (a SELECT that locks the rows it reads: FOR UPDATE on MySQL, PostgreSQL and Oracle, the statement as it is on SQLite, WITH (UPDLOCK, ROWLOCK) table hints here; a trailing FOR UPDATE the driver gets anyway is rewritten the same way). Every driver has them: eZDBInterface writes MySQL's words, each driver names its dialect (sqlHelperDialect()), and the SQL comes from expDBSQLHelpers (mysql, postgresql, sqlite, oracle, sqlserver, generic).

Schema handler

expSQLServerSchema (dbschema.ini, SQLServer and sqlserver) turns .dba schemas into SQL Server DDL and reads a database back: NVARCHAR text, INT IDENTITY for auto_increment, primary keys PK_<table>, defaults DF_<table>_<column> (so a column with a default can be dropped). SQL Server indexes at most 1,700 bytes of a key and no NVARCHAR(MAX) column: a text column in an index key is NVARCHAR(840) (eight kernel columns, such as the URL alias text and path_identification_string); other MAX columns and columns past 1,700 bytes of a non-unique index are included columns. The kernel schema and data were created in SQL Server 2022 with it and read back with no difference.

Installing a site on SQL Server

The database must exist (the installer does not create it on SQL Server); create it with a UTF-8 collation and READ_COMMITTED_SNAPSHOT (above). Then, with sqlsrv, pdo_sqlsrv or PDO_ODBC in the PHP of the command line:

EXP_INSTALL_DB_PASSWORD=secret ./console exp:install --apply --db=sqlserver --db-host=sql1.example.com --db-port=1433 \
  --db-name=site --db-user=exponential --db-encrypt=yes

--db=mssql and --db=sqlsrv say the same. --db-encrypt takes yes (default), no or strict (TDS 8.0, SQL Server 2022 and later); --db-trust-server-certificate accepts a self-signed certificate, for a test server on a network you trust. The web setup wizard and the kickstarter (kickstart.ini, Type=sqlserver, with Encrypt and TrustServerCertificate) offer the same. The installer writes [SQLServerSettings] (Encrypt, TrustServerCertificate) into settings/override/site.ini.append.php as well as into each siteaccess, because the cluster file handler connects before a siteaccess is matched. A connection that fails is logged with its reason and the password masked.

Connection pooling is off. sqlsrv and pdo_sqlsrv keep the socket of a closed connection open for the life of the process; a process that forks after closing one (Velocity's parent after its warm-up, a script with workers) hands that socket to every child. ConnectionPooling=disabled (the default) tells both layers not to pool; on Linux the ODBC driver manager decides by odbcinst.ini ([ODBC] Pooling), so leave that off as well. PDO_ODBC frees the driver with its last connection and forks cleanly.

Read replicas and failover

UseSlaveServer=enabled with the Always On listener (or a secondary) as SlaveServerArray: plain SELECTs of an anonymous GET request go there with ApplicationIntent=ReadOnly (read-only routing). Writes, locks, #temp tables, sequences, session values, transactions and signed-in users stay on the primary. A replica that is not readable (DATABASEPROPERTYEX(..., 'Updateability')) or further behind than SlaveServerMaxLag seconds is not used. For an availability group across subnets set MultiSubnetFailover=enabled.

Tested toolchain (W0)

Measured on 2026-11-24 on an AlmaLinux 10.3 host, SQL Server 2022 CU28 (16.0.4325.4) in Microsoft's Linux container, Developer edition. Every build ran in a throwaway container; the host's PHP configuration was not touched.

PHPsqlsrv / pdo_sqlsrvResult
8.0.305.11.1both build; sqlsrv passes; pdo_sqlsrv 5.11 reads TrustServerCertificate=yes as false (the driver writes 1/0)
8.1.34, 8.2.365.12.0both build and pass (UTF-8 round trip of emoji)
8.3.37, 8.4.28, 8.5.135.13.3both build and pass
8.5.13 (Plesk)5.13.3both build against Plesk's headers and pass; PDO_ODBC (built in) passes too
  • Microsoft ODBC Driver 18 for SQL Server 18.7.1.1 and sqlcmd 18 from Microsoft's RHEL 10 repository run on AlmaLinux 10; ODBC Driver 18 encrypts by default, so a self-signed server needs TrustServerCertificate.
  • PDO_ODBC sends the statement text in the client code page: N'ä' arrived as N'ä', a bound parameter intact. It also prepares every query(), so SET options of such a call end with it. It reads VARCHAR columns in the client code page too (emoji and CJK lost even in a UTF-8 collation): keep text in NVARCHAR. A bound string in a CAST(? AS DATETIME2) is typed by ODBC and failed to convert (22018), so the driver writes the epoch with DATETIME2FROMPARTS(). It sent 2,101 parameters without complaint, where sqlsrv refuses them; the driver's budget of 2,000 applies on every layer.
  • pdo_sqlsrv has MARS on by default; under MARS a BEGIN TRANSACTION batch was rolled back at the end of the batch. The driver's DSN switches MARS off.
  • ODBC's idle connection resiliency (ConnectRetryCount, on by default) reconnected a killed session behind the driver's back; it is off (ConnectRetryCount=0) so that the driver, which knows about #temp tables and open transactions, decides.
  • pdo_dblib: FreeTDS 1.5 is installed on the host, but none of the PHP builds has pdo_dblib; the layer is covered by the DB-less tests only.

Tests

tests/tests/lib/ezdb/sqlserver/ (DB-less, testsuite sqlserver, also part of lib) and .../sqlserver/live/ (testsuite sqlserver-live; skipped without EXP_TEST_SQLSERVER_HOST). The GitHub job sqlserver runs the live tests against SQL Server 2017, 2019, 2022 and 2025 containers on all three layers; sqlserver-windows-localdb is a smoke run on Windows LocalDB when the runner has it.

FileWhat
expSQLServerLexerTestthe tokens (lossless on random bytes)
expSQLServerDialectNamesTestbrackets, 128/116 character names, every reserved word
expSQLServerDialectLiteralsTestescaping, N'' literals, bound parameters, the 2,000 literal budget
expSQLServerDialectPagingTestOFFSET/FETCH in every statement shape
expSQLServerDialectStatementsTeststatement kinds, replica routing, INSERT parsing, identity, temp tables
expSQLServerDialectFunctionsTestupsert, STRING_AGG, timestamps, SUBSTRING, CONCAT, md5, IN lists
expSQLServerConnectionSettingsTestaddresses, DSNs, encryption, session setup, versions, replicas, layers
expSQLServerErrorMapTesterror numbers, SQLSTATEs, retry and transaction rules
expSQLServerDBTestthe driver over a recording transport: what it sends and how it reacts
expSQLServerBuilderPropertyTestproperty tests over random statements
live/expSQLServerLiveTestthe same against a server, per layer (126 cases)

Traceability: the old extension's problems

Each problem found in xrowmssql's ezmssqldb.php, and the tests that keep it from coming back.

#Old problemNowTests
1offset read from the first row and only renumbered: every page repeated the first rowsOFFSET on the serverPG-07, DB-02 testPagingIsSentToTheServer, live testOldOffsetBugIsGone, testPageEqualsSliceOfOrderedRows
2limit applied in PHP over a static cursor of the whole resultFETCH NEXT on the server; limit 0 sends nothingPG-05, testLimitZeroSendsNothing, live testLargeResultSet
3lastSerialID() from @@identity (a trigger's id)SCOPE_IDENTITY() in the same batchtestInsertCapturesScopeIdentityInTheSameBatch, live testTriggerDoesNotChangeLastSerialID, testScopeIdentityInALaterBatchIsLostAfterABoundInsert
4IDENTITY_INSERT switched on and off around every INSERT (three round trips; left on after a failure)only for explicit ids, off again after a failure; NULL/0 left outST-04, testIdentityInsertIsSwitchedOffAfterAFailure, live testExplicitIdsAndIdentityInsertSwitchedOffAfterFailure
5N'' added by regular expressions (gave up on odd quote counts and backslash-quote)lexer; bound parametersLI-03..LI-05, PR-01..PR-03, live testTextRoundTripsBound / AsNLiteral
6reads and writes routed by stristr($sql, 'select')lexer-based isReplicaReadable()ST-02, PR-07, DB-09
7LENGTH( rewritten to LEN( by regular expression (inside literals too; LEN drops trailing spaces)no rewriting; charLength()FN-08, live testTrailingSpacesAndCase
8CREATE TEMPORARY TABLE became a permanent table#table, dropped at request resetST-05, DB-07, live testTemporaryTables
9arrayQuery()'s column by name never workedby name and by positiontestColumnByName, testColumnByPosition
10the identity column lookup never stored its result (a catalog query per insert)read once per connection, again after DDLtestIdentityColumnsAreReadOncePerConnection, ...AgainAfterDdl
11md5() through undocumented fn_varbintohexstr over UTF-16; subString() cut at 8,000CONVERT(..., 2) over UTF-8; no cutFN-04, FN-06, live testMd5EqualsPhp, testMegabyteOfText
12sqlsrv only on Windows, dblib only elsewhere, MySQL charset names, begin/commit errors ignoredfour layers anywhere, UTF-8 only, errors mappedCN-08, ER-*, DB-05
Dialect and theory pointTests
reserved words, [] quoting, 128/116 character namesNM-01..NM-03, live testNameLongerThan128IsRefusedByTheServer
2,100 parameterstestMoreThanTheParameterBudgetFallsBack, live testServerRefusesMoreThan2100Parameters, testManyStringLiteralsStillWork
900/1,700 byte index keys, NVARCHAR(MAX) not a keylive testIndexKeyLimits
empty string is not NULL; trailing spaces; case-insensitive default collationlive testEmptyStringIsNotNull, testTrailingSpacesAndCase
VARCHAR in a Latin collation loses characters; UTF-8 collation keeps themlive testVarcharInALatinCollation..., testVarcharInAUtf8Collation...
deadlock 1205, lock timeout 1222, RCSI readerslive testDeadlockVictimAndRetry, testLockTimeout, testReaderIsNotBlockedByWriterWithRcsi
reconnect after a lost sessionDB-06, live testKilledSessionIsOpenedAgain, testKilledSessionInsideTransactionFails
dates past 2038FN-03, live testUnixTimestampRoundTrip
MS Access, generic ODBClater waves: the ODBC DSN rules (CN-02, CN-04) are shared

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x