How to Fix SQL Server Error 15661 (Msg 15661)
You trigger sql server error 15661 when you run compression checks on a temporary table. To fix it, run the estimation procedure only on permanent user tables in your database. Database teams often check how much disk space they can save with data compression. SQL Server includes a built-in system stored procedure to estimate these space savings. Still, passing a temporary table name into this tool causes the engine to fail. The database returns message 15661 with a severity level of 16. This s
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 trigger sql server error 15661 when you run compression checks on a temporary table. To fix it, run the estimation procedure only on permanent user tables in your database.
Database teams often check how much disk space they can save with data compression. SQL Server includes a built-in system stored procedure to estimate these space savings. Still, passing a temporary table name into this tool causes the engine to fail. The database returns message 15661 with a severity level of 16. This severity level means the user made a mistake that they can fix.
What causes sql server error 15661 in the database engine
The official message for sql server error 15661 states the exact limit clearly. Compressing temporary tables is not supported by the stored procedure sp_estimate_data_compression_savings. When you run this procedure, the engine samples your table data in the background. It copies a small subset of rows into a hidden workspace to test compression. It then compares the size of the original data against the compressed sample.
Temporary tables already live inside the system temporary database. Because of how SQL Server manages temporary objects, the estimation tool blocks them right away. This happens whether you use local temporary tables or global temporary tables. Automated maintenance scripts often hit this error when they loop through all table names. If a script fails to filter out temporary objects, the batch job stops with message 15661.
How to diagnose and fix the failure in production
First, find the script or query that called the compression estimation procedure. Check the table name parameter passed into the procedure call. Look for names that start with one or two hash symbols. Those symbols mark local and global temporary tables in SQL Server. Also verify which database context the session uses when it runs the command.
If your goal is to test compression on sample data, use a permanent staging table. Create a regular user table in a test database or a dedicated schema. Load your sample rows into that permanent table and run the estimation procedure there. Once you finish checking the space savings, drop the staging table to clean up. If an automated script caused the error, update its query filter. Make sure the script skips temporary objects and system databases before running estimates.
Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Managing permissions when testing compression in production
Never grant excessive privileges just to debug a failed maintenance script. Many teams give out the db_owner fixed role when a script fails in production. Still, members of db_owner can perform all maintenance actions and even drop the database. Instead, keep user permissions tight while you check table sizes and fix your scripts.
In SQL Server, a database user is the identity of a login in a database. For people who need access to many databases, a login in master is common. For access to one or a few databases, a contained database user is an option. An admin must first enable contained databases on the server and the database. You cannot change the permissions assigned to fixed database roles. Assign users to db_datareader if they only need to read data from user tables. If they must load test rows into a staging table, add them to db_datawriter. Also make sure your production results mask sensitive fields like emails, phone numbers, and card numbers. Rewriting matched fields in query results protects private data while engineers inspect staging tables.
Add runtime guardrails beside SQL Server
Role grants and network rules are needed setup for SQL Server. Still, an allowed user or agent tool can send a bad delete or read raw customer fields over a valid session. In practice, hoop.dev sits inline in the TDS protocol beside the database as a sidecar for your SQL Server. For every query an agent or human runs in production, hoop.dev decides whether to pass, mask, block, or escalate it for review.
With hoop.dev SQL Server controls, rules refuse banned statements before they run, hold risky queries for human review, and mask matched sensitive values in supported result columns in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for HIPAA, PCI DSS, SOX, and GDPR audits. See the source on GitHub.
Frequently asked questions
What does severity 16 mean for message 15661?
Severity 16 indicates a general error that the user or developer can correct. It does not mean your database is corrupt or that the server crashed. You simply passed an unsupported temporary object into the system stored procedure.
Can I compress a temporary table directly without estimating savings?
Error 15661 only applies to the estimation stored procedure sp_estimate_data_compression_savings. The estimation procedure itself refuses to process temporary tables as an input target. To use that estimation tool, you must point it at a regular user table.
What database role should engineers use to inspect user tables safely?
Use the db_datareader fixed database role for engineers who only need to read table data. Members of db_datareader can read all data from all user tables and views. Avoid granting db_owner because that role allows users to drop the entire database.
Related guides
In this post