If you’re getting SQL Server error 2788 that reads “Synonyms are invalid in a schemabound object or a constraint expression” it seems that you’re trying (whether intentionally or not) to create a schemabound object that includes a synonym in its definition, or a constraint with a synonym in its expression.
This could happen if you try to include an object from a non-default schema, but you omit the schema name in the definition or constraint expression. If there’s an object with the same name in the default schema, then SQL Server will try to use that instead of the one you intended for it to use.
To fix this issue, be make sure you don’t include a synonym in your schemabound objects or constraint expressions. This could mean including the schema name when you reference the object.
Example of Error
Here’s an example of code that produces the error:
CREATE VIEW vCustomers WITH SCHEMABINDING AS
SELECT
CustomerId,
CustomerName,
Email
FROM Customers;
Output:
Msg 2788, Level 16, State 1, Procedure vCustomers, Line 2
Synonyms are invalid in a schemabound object or a constraint expression.
This causes the error, due to one of the aforementioned reasons.
Here, I’m trying to create a view, but my reference to Customers
is causing an issue. In my case, I have a Customers
table and a Customers
synonym, but they belong to different schemas. The table is at Sales.Customers
and the synonym is at dbo.Customers
.
SQL Server thinks that I’m referring to the synonym, but my intention is to refer to the table.
Solution
We can easily fix this problem by using the two-part naming format (i.e. including the schema in the name):
CREATE VIEW vCustomers WITH SCHEMABINDING AS
SELECT
CustomerId,
CustomerName,
Email
FROM Sales.Customers;
Output:
Commands completed successfully.
This time it worked without issue; the view was created.