Security Considerations for AI-Assisted SQL Development
This lesson explains what AI-assisted tools send to model services, the risks in generated code and the least-privilege controls for agents.
Learning outcomes
After completing this lesson, you will be able to:
- Identify what data AI-assisted tools send to model services.
- Describe common risks in AI-generated T-SQL.
- Explain least-privilege controls for agents that use database tools.
What leaves the environment
A chat or completion request sends a prompt plus context that the tool gathers: open files, selected code, repository instructions and, when connected to a database, schema metadata and sometimes query results. That content is processed by a model service. Organizations therefore decide which tools and models are permitted, use business or enterprise plans whose prompts are not used to train models, and configure content exclusions for sensitive files. Connection strings, keys, passwords and personal data must never be pasted into prompts or stored in instruction files, and sample data used for testing prompts should be synthetic.
What reaches the model
Risks in generated code
Generated T-SQL can look plausible and still be wrong or unsafe. Common problems include dynamic SQL built by concatenating input (SQL injection), UPDATE or DELETE statements without a selective WHERE clause, NOLOCK hints that return inconsistent data, overly broad GRANT statements, functions that do not exist on the target platform and queries that perform poorly at scale. Generated code goes through the same controls as human code: pull request review, build validation in a SQL project, code analysis, tests and a deploy report before production.
Reviewing generated T-SQL
Agents, tools and least privilege
In agent mode, the model can call tools that run queries, edit files or invoke MCP servers. A tool acts with the permissions of the identity it runs as, so the main control is least privilege, as the following list describes.
- Connect agents with a dedicated database user that has only the needed permissions, often read-only or limited to specific schemas, objects or procedures, never with an administrator login.
- Point agents at development or test databases, or at masked copies, rather than production data.
- Keep approval prompts for tools that write data or change schema, and review what each call will do before approving it.
- Configure only trusted MCP servers, because a server is code that runs with access to its data source.
- Treat tool output as untrusted input: text in data or documents can contain prompt injection that tries to make the agent take other actions.
Safe agent set-up?
An agent for schema exploration
A team wants developers to ask Copilot agent mode questions about a production database's schema and sample rows. The database contains customer personal data, and the security team requires that the agent cannot change data or see personal information. What should the developer configure?
Connect the agent to a non-production copy with masked or synthetic personal data, or, if production is unavoidable, through a dedicated user with SELECT on approved views that exclude personal columns and with Dynamic Data Masking on the rest, and keep approvals for every tool call. Using a developer's own db_owner login would give the agent every permission the developer has, and relying on the prompt to ask the model not to modify data is not a security control.
Why is not an instruction telling the agent "never modify data" a security control?
Show answer
The model may not follow it, and prompt injection can override it; only the identity's permissions and approvals reliably limit what tools can do.
Exam Tips
Remember for the exam
- Agents act with their identity's permissions: use a least-privilege, preferably read-only, user.
- Never put secrets or personal data in prompts or instruction files.
- Review and test generated SQL like any other code.
- Instructions to the model are not security controls; permissions are.