How to Fix SQL Server Error 12306 (Msg 12306)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get sql server error 12306 when a query uses cursors with unsupported database features. You fix this error by removing the cursor and using set-based queries instead. Database engines return this message with a severity level of 15. A severity 15 message means your query has a syntax or feature rule error. The engine blocks the batch before it runs any changes on your data. What causes sql server error 12306 in the database engine The official message says that cursors are not supported

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You get sql server error 12306 when a query uses cursors with unsupported database features. You fix this error by removing the cursor and using set-based queries instead.

Database engines return this message with a severity level of 15. A severity 15 message means your query has a syntax or feature rule error. The engine blocks the batch before it runs any changes on your data.

What causes sql server error 12306 in the database engine

The official message says that cursors are not supported with a specific feature. SQL Server replaces the placeholder at the end with the exact feature name. This often happens with memory-optimized tables or natively compiled stored procedures.

Standard relational tables allow row-by-row processing through server cursors. Newer engine features focus on high speed and lock-free execution. They drop cursor support to keep transactions fast and simple.

Client drivers can also trigger this error without your knowledge. Some reporting tools wrap every query in an API server cursor by default. When that tool queries an unsupported object, the engine throws message 12306.

Upgrading legacy databases to newer table types often reveals this problem. Old stored procedures that loop through rows fail during testing. Developers must find every place where row-by-row logic touches restricted objects.

How to diagnose and fix the failure in production

Start by reading the full error text in your app logs. The last words of the message name the exact feature that failed. Next, find the batch or stored procedure that sent the query.

You do not need high database rights to find the broken code. Avoid giving developers the fixed database owner role in production. That role can do all maintenance tasks and even drop the database.

Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Instead, grant read-only access so engineers can inspect data safely. Members of the fixed data reader role can read all user tables and views. You cannot change the permissions assigned to fixed database roles.

You also must decide how the engineer logs in to the server. A database user is the identity of a login inside a database. A SQL user tied to a master login works well for many databases. For one or a few databases, you can use a contained database user. An admin must enable contained databases on the server and database first.

Once you find the query, rewrite the cursor logic as a set-based operation. Use standard joins, aggregations, or temporary work tables to process rows together. If you must loop, load the target rows into a local variable table first. Then iterate using a simple loop instead of declaring a formal cursor.

If a client app caused the error, check the driver settings. Switch the driver from server cursors to direct forward-only result sets. The query will then run cleanly without changing your database schema.

Preventing cursor errors across database teams

Teams can stop this error early by checking queries before deployment. Add static code checks to your build pipeline to flag cursor declarations. Encourage engineers to write set-based SQL for all new app features.

When teams run ad-hoc checks in production, keep data safety in mind. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs. This keeps sensitive values safe while engineers test query fixes.

Check SQL Server batches and results on the wire

SQL Server logins and roles decide who connects. They do not check what each TDS batch does or mask sensitive values in returned rows. With hoop.dev, you run an open-source proxy, a sidecar for your SQL Server, inline in the TDS wire protocol. On every batch an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Before a batch hits the server, hoop.dev guardrails refuse disallowed actions with a native TDS error or hold risky statements for approval. On the way back, hoop.dev data masking rewrites sensitive values in supported result columns in memory, and every decision is logged. Explore the MIT-licensed project on GitHub.

Frequently asked questions

What does severity 15 mean for SQL Server error 12306?

Severity 15 means the error is a syntax or command rule mistake. Users can fix it by changing the SQL query code directly. It does not mean the server hardware or database files are broken.

Can a client driver trigger Msg 12306 without a cursor in the SQL text?

Yes, client drivers can ask the server to open an implicit API cursor. If the query touches a feature that bans cursors, the engine returns error 12306. Changing the driver result set mode fixes the issue.

What is the safest way to replace a cursor in a stored procedure?

The best fix is to rewrite the logic as a single set-based statement. If row-by-row work is still needed, use a while loop over a table variable. Both methods avoid formal cursors and clear the error.

The query runs. The emails stay hidden.