SQL Server Error 1081: Fix Database Name Issue
In this article
SQL Server error 1081 occurs when you try to specify a database name in a statement or operation that does not allow it. The full message is typically: "The database name '%.*ls' is not allowed in this context." This usually happens with certain backup or restore commands, or when using a database name where a logical file name or other identifier is expected. The fix is to identify the correct syntax for the operation you are running and remove the database name from the clause that rejects it. Below is a step-by-step guide to resolve the error, along with common pitfalls and troubleshooting advice.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or another client that can run T-SQL queries.
- Appropriate permissions to perform the operation (e.g., ALTER DATABASE, BACKUP, RESTORE).
- The exact error message and the T-SQL statement that triggered it.
Step-by-Step
- Read the full error message. Error 1081 often includes the offending database name and the context. Note the line number and the statement that caused it. This tells you which clause is rejecting the database name.
- Identify the operation. Common scenarios include:
BACKUP DATABASE ... WITH MIRROR TO ...– specifying a database name in the mirror file list.RESTORE DATABASE ... WITH MOVE ...– using a database name where a logical file name is expected.ALTER DATABASE ... ADD FILE ...– including the database name in the file specification.- Using a database name in a
DBCCcommand that expects a logical file name.
- Check the syntax for that operation. Consult the official SQL Server documentation for the exact command you are using. Pay attention to parameters that expect a logical file name, a physical path, or a database name. For example, in
RESTORE DATABASE, theMOVEclause requires logical file names, not the database name. - Remove or replace the database name. If the clause expects a logical file name, use the logical name from
sys.database_filesorsys.master_files. For a backup mirror, ensure you are not including the database name in theMIRROR TOlist. For file operations, use the logical file name instead of the database name. - Rewrite the statement. For example, a common mistake is:
The correct form uses the logical file name (often the same as the database name for the primary file, but not always). Check with:RESTORE DATABASE MyDB FROM DISK = 'C:\backup.bak' WITH MOVE 'MyDB' TO 'C:\data\MyDB.mdf';
Then use theRESTORE FILELISTONLY FROM DISK = 'C:\backup.bak';LogicalNamecolumn value in theMOVEclause. - Test the corrected statement. Run it in a non-production environment first if possible. Verify that the error no longer appears and the operation completes successfully.
- Review other statements. If the error occurred in a script, search for similar patterns elsewhere to prevent recurrence.
Common Mistakes
- Confusing logical file names with database names. The database name is not always the same as the logical name of its files. Always verify using
sys.database_filesorRESTORE FILELISTONLY. - Using the database name in a
MIRROR TOclause. TheMIRROR TOclause expects a file path, not a database name. - Assuming all commands accept a database name. Some commands, like
DBCC SHRINKFILE, require a logical file name, not the database name. - Copying scripts from older versions. Syntax may have changed; always check current documentation.
- Ignoring the full error text. The error often points to the exact token that is invalid.
Tips
- Use
RESTORE FILELISTONLYbefore a restore to get the correct logical file names. - When scripting backups, use variables for file paths and logical names to avoid hardcoding errors.
- Keep a reference of common T-SQL commands and their parameter requirements.
- Test scripts in a development environment before running in production.
- Use SQL Server Management Studio's IntelliSense to catch syntax errors early.
Troubleshooting
If you still encounter error 1081 after correcting the statement, consider the following:
- Check for dynamic SQL. The database name might be concatenated into a string. Print the generated statement to inspect it.
- Verify permissions. Lack of permissions can sometimes produce misleading errors. Ensure you have the necessary rights.
- Look for triggers or jobs. The error might originate from a scheduled job or trigger that runs a problematic statement.
- Use Profiler or Extended Events. Trace the exact statement being executed to identify the source.
- Consult the documentation. If the error persists, review the official Microsoft docs for the specific command.
Frequently Asked Questions
What does error 1081 mean?
Error 1081 indicates that a database name was specified in a context where it is not allowed, such as a clause that expects a logical file name or a file path.
How do I find the logical file names for a database?
Query sys.database_files in the database, or use RESTORE FILELISTONLY on a backup file.
Can error 1081 occur during backup?
Yes, typically when using MIRROR TO and incorrectly including a database name instead of a file path.
Is error 1081 specific to SQL Server?
Yes, it is a SQL Server error code.
What should I do if I cannot resolve error 1081?
Double-check the syntax against official documentation, simplify the statement, and test incrementally. If needed, seek help from SQL Server communities.