Error 218 in SQL Server occurs when the system cannot locate a specific type (like a user-defined table type, function, or view) referenced in your query, stored procedure, or trigger. This typically happens due to missing declarations, incorrect references, or schema changes. Here’s how to resolve it immediately and permanently.
What You'll Need
- A working SQL Server instance (local or remote), whether Express, Standard, or Enterprise.
- Access to SQL Server Management Studio (SSMS) or a tool like Azure Data Studio.
- Admin or sufficient permissions to check and modify object definitions.
- Your script or query file containing the error.
Step-by-Step
-
Verify the object exists.
Run this query in SSMS to check if the referenced type exists in the correct schema:
SELECT * FROM INFORMATION_SCHEMA.ROUTINE_COLLECTIONS WHERE ROUTINE_TYPE = 'TABLE TYPE' AND ROUTINE_NAME LIKE '%YourObject%'If the object isn’t found, proceed to step 3. If it exists but still throws the error, move to step 4. -
Check for typos or case sensitivity.
SQL Server is case-sensitive in some contexts. Ensure the exact name is used:
SELECT OBJECT_NAME(1) AS SchemaName, OBJECT_NAME(1) AS TableName FROM sys.tables WHERE name LIKE '%YourTable%' COLLATE SQL_Latin1_General_CP1_CI_ASIf the case differs, update your query or stored procedure to match the schema’s naming convention. -
Recreate the missing type.
If the object is truly missing, create it with the correct schema and permissions:
CREATE TYPE YourSchema.YourTableType AS TABLE (Column1 datatype, Column2 datatype);ReplaceYourSchemawith the schema where the type should reside (default isdbo). -
Refresh the query plan cache.
Sometimes cached metadata causes the error. Restart SSMS or clear the cache:
1. Press
Ctrl+Shift+F5to refresh the connection. 2. If the issue persists, restart the SQL Server service via Services.msc. -
Validate schema changes.
If you recently altered the database schema (e.g., dropped an object), check for orphaned references:
EXEC sp_depends @objname = 'YourStoredProcedure';If the query shows dependencies that no longer exist, update or remove the references accordingly. -
Recompile the procedure or script.
If the error persists in a stored procedure, recompile it:
1. Right-click the procedure in Object Explorer →
Script as→Create To→New Query Editor Window. 2. Explicitly set the option to recompile:OPTION (RECOMPILE);
Common Mistakes
- **Using an outdated object name.** This often happens after schema changes. Always verify object names post-updates.
-
**Ignoring schema scope.** SQL Server resolves types in the schema where they’re defined. A table type in
dbowon’t work in another schema unless explicitly qualified. -
**Caching issues.** SSMS or SQL Server’s cache may delay updates. Use
SET QUOTED_IDENTIFIER OFFtemporarily to force parsing. -
**Permissions misconfiguration.** Ensure your login has
SELECTorALTERpermissions on the schema/object.
Tips
- **Use dynamic SQL for complex queries.** If the error involves dynamic T-SQL, ensure the resolved object is referenced early in the script.
-
**Check the error context.** Error 218 often fails in triggers or views. Run
EXEC sp_helptext 'YourProcedure'to see where the error occurs. - **Test in a staging database.** If the issue persists in production, verify the fix in a mirrored environment first.
-
**Enable error tracing.** Enable the SQL Server error log or use
SELECT @@ERRORto capture the full error stack.
If That Did Not Work
If none of the above steps resolve the issue, try these advanced steps: 1. **Check for conflicting objects:** Run this to see if another object with the same name exists:SELECT OBJECT_NAME(object_id) FROM sys.objects WHERE name LIKE '%YourObject%' AND type_desc = 'USER_DEFINED_TABLE'
2. **Recreate the schema:**
If the schema is corrupted, recreate it via SQL Agent or a backup restore.
3. **Use a different client tool:**
Try Azure Data Studio or DBeaver to test the query—some SSMS-specific bugs may appear.
Troubleshooting
If you’re still stuck, follow these steps systematically:
- Restart the SQL Server service. Sometimes, hanging processes cause metadata to fail.
- Reinstall SQL Server. Only do this as a last resort, ensuring you back up critical data first.
- Contact Microsoft Support. If the issue persists beyond schema changes, it may be a rare bug. File a support ticket with the SQL Server error number (218) and reproduction steps.
Frequently Asked Questions
Why does SQL Server Error 218 keep happening in my script?
The most likely cause is an undocumented schema change (e.g., a table type was dropped without updating references) or a typo in your query. Always validate object names after schema modifications.
How do I force SQL Server to ignore case sensitivity in object names?
SQL Server does not natively support case-insensitive object names. Use collation hints (e.g., COLLATE SQL_Latin1_General_CP1_CI_AS) or standardize naming across your team.
What should I do if the error occurs in a stored procedure I didn’t write?
Check for dependent objects using sp_depends. If the procedure relies on a broken type, either update the procedure or fix the missing object.
Can Error 218 be caused by a full-text search query?
No, this error pertains to object metadata, not full-text operations. However, a full-text index issue could indirectly cause query failures—verify the query’s scope.
How do I check if a type is registered in SQL Server’s type catalog?
Run:
SELECT CAST(name AS NVARCHAR(128)) FROM sys.types WHERE is_user_defined = 1 AND name LIKE '%YourType%';
If nothing appears, the type is not registered.
Will a simple restart of SSMS fix this error?
Sometimes. Restarting refreshes the query plan cache, but if the issue stems from a schema conflict, you’ll need deeper fixes (e.g., recreating objects).