If you run SQL Server for a living, you already speak AI. You just use different words. A prompt is a query. A context window is a memory grant. A tool call is a stored procedure call. And an agent? That's a junior DBA with a runbook.
I made the Oracle version of this video first. This is the SQL Server one, written down. Same idea: no product tour, just a map from what you already know to what's new, and where it can hurt you.
2:15 at night, and the log won't clear
The running example is one we've all had. SalesDB's transaction log is at 97% and it just won't clear. The old way: you open your script folder, your notes, a couple of old blog posts and a search engine. You write the query against sys.databases yourself, run it in SSMS or sqlcmd, and read the result. Every step is yours.
Now you can type the question in plain English. The model drafts the T-SQL, explains the wait or the plan, and an agent can even run read-only queries through a tool. What didn't change: the database is still the source of truth, and you're still the one accountable for what runs on it.
On-prem or Azure doesn't matter much for the AI part. What matters is where the model runs and where your data goes. A local model keeps the data inside. A cloud model is usually stronger, but your prompt leaves the building.
The translation table
| AI term | What you already know | Why it matters |
|---|---|---|
| Prompt | A query with no parser | SQL Server throws Msg 102 on bad syntax. A model just guesses. "Which database uses the most log?" Megabytes or percent? Write prompts like a good ticket. |
| Tokens | 8 KB data pages | The unit of work, cost and every limit. Longer prompts cost more, like more logical reads in STATISTICS IO. |
| Context window | Memory grant | Fixed size. When a long chat fills it, the app trims, a bit like a sort that doesn't fit its grant and spills to disk. The middle of a long context gets lost. |
| Stateless API | A pooled connection after reset | A chat only seems to remember because the app resends the whole history every time. |
| System prompt | Logon trigger ON ALL SERVER | Applied before every request. Put the dialect in it: T-SQL, not Postgres. |
| Hallucination | Stale statistics | Confident and wrong. In the video the model invents a column called log_wait_reason. The real one is log_reuse_wait_desc. |
| Non-determinism | No Query Store to force a plan | Same question, different T-SQL. Test against a case you already know. |
| Grounding / RAG | Catalog lookup | Feed only the slice it needs: sys.columns, sys.foreign_keys, a runbook page. |
| Tool call | Stored procedure call | The model only asks. The app executes. The model never touches the database. |
| MCP | Port 1433 + ODBC/JDBC driver | One protocol, many clients and servers. Microsoft ships SQL MCP Server in Data API builder. |
| Workflow vs agent | SQL Agent job vs junior DBA | A job has fixed steps. An agent picks its next step from each result. |
| Human-in-the-loop | Change approval | The agent finds the open transaction. KILL is still your call. |
Two queries, one question
"Who holds the oldest open transaction?" I got two answers that both ran fine. One sorted sys.dm_exec_sessions by login_time. That's the oldest login, which may not hold any transaction at all. The other sorted sys.dm_tran_database_transactions by database_transaction_begin_time. Only that one answers the question. This is why you check the result against a small case you already know.
Where the agent stops
SELECT name, log_reuse_wait_desc
FROM sys.databases
WHERE name = N'SalesDB';
-- ACTIVE_TRANSACTION
DBCC OPENTRAN (SalesDB);
-- KILL 87; → you approve
Ask why, ask who, then stop. Killing that session rolls back its work, and that's a decision a human makes.
What got better, what it costs
The good part: a first T-SQL draft in seconds, plain-English explanations of plans, wait stats and error messages, help on an engine you know less well, and a lot less searching. The bad part: answers can be confidently wrong, the same question can give different results, calls cost tokens and time, and data may leave your network. NIST also names a quieter one, over-reliance. You trust it because it sounds sure, and slowly stop checking.
Security, in DBA words
- Prompt injection is SQL injection without parameters. A value in a table row or a line in the error log can say "ignore your rules". With T-SQL you'd fix it with
sp_executesqland parameters. There's no such thing for prompts, so limit what the model can do. OWASP lists it as LLM risk number one. - Sensitive data disclosure is an uncontrolled export. Classify before you paste, mask real values, use a local model for anything you wouldn't send outside.
- Excessive agency is putting an app login in sysadmin. Give the AI its own login, something like
db_datareader, and require human approval for anything high impact. - Avoid the lethal trifecta: private data, untrusted content and a way to send data out. Never all three in one agent.
- Passwords and connection strings never go into the chat.
- Audit it like any other login. A separate login means
sys.fn_get_audit_fileshows exactly what it ran. Give the connection its own application name andprogram_nameinsys.dm_exec_sessionstells you which session is the AI. - Production last. Sanitized read-only copy, lab first, human approval, and a database snapshot or a fresh backup before any change.
New words, old habits. Least privilege, review before commit, test before production. If you've done this job for a while, you already have most of the instincts you need.
Full video: You Already Speak AI (SQL Server edition) on YouTube. Session IDs and outputs in the video are illustrative.
Sources
- OWASP Top 10 for LLM Applications 2025: genai.owasp.org/llm-top-10
- Model Context Protocol specification: modelcontextprotocol.io/specification
- Anthropic, Building effective agents: anthropic.com
- Simon Willison, The lethal trifecta for AI agents: simonwillison.net
- Liu et al., Lost in the Middle (2023): arxiv.org/abs/2307.03172
- NIST AI 600-1, Generative AI Profile: doi.org/10.6028/NIST.AI.600-1
- SQL MCP Server (Data API builder): learn.microsoft.com
- Microsoft Learn: sys.databases, DBCC OPENTRAN, sys.dm_exec_sessions, sys.dm_tran_database_transactions, memory grants, sp_executesql, sys.fn_get_audit_file, database snapshots, KILL
Comments
Post a Comment