🗄️ SQL Server: Fix a Synonym Pointing Past a Renamed Database
A SQL Server synonym referencing an object in another database stores the literal database name as a string at creation time — it doesn’t track a renamed database the way a foreign key tracks a renamed column. Renaming the target database during a migration or environment refresh leaves every synonym pointing at a name that no longer exists, so queries going through the synonym fail with “invalid object name,” which reads like the object itself vanished when only the database’s name actually changed.
🔎 The Problem
CREATE SYNONYM dbo.Customers FOR [Reporting_Old].[dbo].[Customers]; ALTER DATABASE [Reporting_Old] MODIFY NAME = [Reporting]; SELECT TOP 1 * FROM dbo.Customers; -- Msg 208: Invalid object name 'dbo.Customers' -- (synonym still points at 'Reporting_Old', which no longer exists)
✅ Fix: Find Every Synonym Referencing the Old Name and Recreate It
- After renaming a database, query
sys.synonymsfor any synonym whose base object name references the old database name, and recreate each one pointing at the new name — a rename doesn’t update any synonym definitions automatically. - Prefer a consistent, environment-independent database name for anything cross-database synonyms depend on, so a future rename doesn’t require hunting down every synonym that references it by name.
- Add a post-rename verification step that actually executes a query through every synonym pointing cross-database, rather than just confirming the rename itself succeeded — the rename can succeed while every dependent synonym silently breaks.
🚨 Why This Is Dangerous
- An application that queries through a synonym rather than a direct table reference can fail everywhere that synonym is used, all at once, right after a database rename that looked completely unrelated to anyone who didn’t know the synonym existed.
A synonym remembers the name you gave it at creation time, not the object you meant — rename the database, and it keeps its promise to the wrong string.



