How to connect SQL Server to ChatGPT with a deterministic session contract
A SQL Server login does not define a complete execution environment.
The same text can parse dates differently under another language or DATEFORMAT. Week calculations change with DATEFIRST. ANSI settings affect errors and expression behavior. A pooled connection can also return with transaction or session state left by the previous request.
When you connect SQL Server to ChatGPT, make those settings a deterministic contract before trusting the first answer.
The hidden inputs to a SQL Server query
Alongside SQL text and parameters, the result can depend on:
- authenticated login, execution context, default database, and default schema;
SET LANGUAGE,SET DATEFORMAT, andSET DATEFIRST;- ANSI settings such as
ANSI_NULLS,ANSI_WARNINGS,QUOTED_IDENTIFIER, andARITHABORT; - transaction isolation and open transaction state;
XACT_ABORT, lock timeout, command timeout, and cancellation behavior;- database and column collation.
Some options also affect plan selection or whether indexed objects can be used. A query can therefore be logically and operationally different even when its text looks identical.
Define one reviewed baseline per workflow
Start with the business operation, then record:
- approved server, database, schemas, views, and stored operations;
- database login or service identity and least-privilege grants;
- language, date format, first day of week, and business timezone/cutoff;
- required ANSI and error-handling options;
- transaction isolation, lock timeout, command timeout, row limit, and cancellation policy;
- tool, schema, metric, and session-contract versions.
Keep the baseline in trusted connector configuration. Model text must not be able to change the language, widen the database scope, disable error behavior, or select a stronger role.
For the general architecture, see MCP server access for SQL Server.
Remove ambiguous date parsing
Never ask the model to generate locale-dependent date literals such as 03/04/2026. Use typed parameters and unambiguous date/time values. Translate “this week” or “last month” into explicit start and end instants under an approved business calendar.
Set and verify language, DATEFORMAT, and DATEFIRST for operations that depend on them. Better still, make week and period definitions part of a governed semantic layer so a session default cannot redefine a finance or operations metric.
Pin ANSI and error semantics
Warnings and arithmetic failures should not disappear merely because another driver or connection default was used. Choose the required ANSI settings, ARITHABORT, and XACT_ABORT deliberately, then test the behavior that matters for the workflow and SQL Server version.
A generated query that violates the baseline should fail clearly. Do not toggle options dynamically until it passes; that hides a contract mismatch and may alter both correctness and execution plans.
Fully qualify the approved data surface
Default schema is convenient for humans and risky for generated SQL. Expose reviewed views or procedures, and use schema-qualified names such as reporting.monthly_revenue. Restrict discovery to approved objects.
Test whether an object with the same name in another schema could be selected, whether synonyms cross an unexpected boundary, and whether ownership chaining or execution context grants more access than the connector role should have.
Use the schema-discovery permissions contract for the metadata side.
Reset pooled connections before reuse
Driver pooling can make connection setup fast, but the application still owns the invariants it depends on. A prior operation may leave an open transaction, changed isolation, session context, temporary tables, or other workflow state.
On checkout, confirm there is no unexpected transaction, establish the reviewed settings, set trusted session context if used for policy, and verify the effective identity and database. On return, rollback incomplete work and discard a connection that cannot be proven clean.
Do not trust a single startup initialization statement when the connection can live longer than the process that first configured it.
Keep identity outside model text
User, tenant, role, environment, and purpose should come from authenticated runtime context. If SQL Server session context or a trusted procedure carries those values, set them through the connector and prevent generated SQL from overriding them.
Back the rule with database permissions, row-level security, approved views, or procedures. Prompt instructions are not authorization.
See how to preserve user identity through a ChatGPT database connection.
Bound transactions and cancellation
An AI conversation is not a database transaction. Keep each approved database operation short, apply command and lock timeouts, propagate cancellation, and release the connection promptly.
Define what a timeout means: no work started, rolled back, committed, or indeterminate. For any mutation-capable workflow, use separate tools, idempotency, approval where required, and a durable receipt. Read-only should remain the default for question-answering.
Fingerprint the effective session
Before an operation runs, compare the effective database, identity, schema scope, language/date settings, required ANSI options, isolation, and policy context with the expected version. Record a normalized fingerprint rather than raw connection secrets.
Return a structured configuration error when the fingerprint is wrong. That is safer than producing an answer under unknown semantics.
Build a session-bound acceptance suite
- Run the same question on new and reused pooled connections.
- Change login language and date defaults; require identical governed period boundaries.
- Vary
DATEFIRSTand test weekly metrics. - Remove one required ANSI option and require rejection.
- Leave a transaction or session-context value behind before pool return.
- Create same-named objects in another schema and verify qualification.
- Force a lock timeout, command timeout, cancellation, and arithmetic error.
- Repeat after driver, pool, SQL Server, schema, or policy changes.
Check normalized answers, refusals, database traces, and the session fingerprint—not only HTTP success.
Make the answer reproducible
Include source database, approved object, authenticated role, business period and timezone, contract version, row count, truncation, freshness, policy decision, and trace ID in the result evidence. This gives reviewers the context needed to reproduce the operation without exposing credentials.
Where Conexor fits
Conexor provides MCP infrastructure for connecting ChatGPT and other AI clients to databases and APIs through governed tools. A SQL Server session contract makes runtime semantics, identity, scope, limits, and evidence visible as pooled connections move between AI requests.
Explore the ChatGPT database connector
For setup guidance, continue with the SQL Server MCP connection guide.