r/SQLServer • u/andrewdp23 • 19d ago
Question Linked server query fails when remote table with CHECK constraint is aliased
I'm trying to work out whether I've hit a SQL Server regression or just an obscure linked-server behaviour.
Changing the alias of a remote table in a query is causing the query to fail to compile because of a column reference in a CHECK constraint on the remote table.
I've tested this on SQL Server 2025 CU9 and SQL Server 2019 CU32 + GDR.
Example setup on SERVER1:
CREATE DATABASE TESTDB
GO
USE TESTDB
CREATE TABLE [dbo].[TESTTABLE](
[DOB] [datetime] NULL
)
ALTER TABLE [dbo].[TESTTABLE] WITH CHECK
ADD CONSTRAINT [CK_TESTTABLE_Age]
CHECK ([TESTTABLE].[DOB] IS NOT NULL )Linked server call from SERVER2, where SERVER1 is a linked server:
Executing without an alias:
SELECT *
FROM SERVER1.TESTDB.dbo.TESTTABLESucceeds with 0 rows returned.
Aliasing to the same name:
SELECT *
FROM SERVER1.TESTDB.dbo.TESTTABLE AS TESTTABLESucceeds with 0 rows returned.
Aliasing to a different name:
SELECT *
FROM SERVER1.TESTDB.dbo.TESTTABLE AS TESTNEWFails with:
Msg 4104, Level 16, State 1, Line 51
The multi-part identifier "TESTTABLE.DOB" could not be bound.Explicitly referencing the failing column:
SELECT TESTNEW.DOB
FROM SERVER1.TESTDB.dbo.TESTTABLE AS TESTNEWFails with:
Msg 4104, Level 16, State 1, Line 51
The multi-part identifier "TESTTABLE.DOB" could not be bound.Executing the query remotely works:
EXEC('
SELECT *
FROM TESTDB.dbo.TESTTABLE AS TESTNEW
') AT [SERVER1]Succeeds with 0 rows returned.
If I wrap that remote table reference in a view as below, it succeeds.
CREATE VIEW dbo.TESTNEW AS
SELECT *
FROM SERVER1.TESTDB.dbo.TESTTABLE
GO
SELECT *
FROM dbo.TESTNEW AS XXXXXRemoving the CHECK constraint from a test copy of the table resolves the error, as does removing the table name from the column reference in the CHECK constraint.
I've ruled out a few possible causes:
- This happens over SQLNCLI11 and MSOLEDBSQL
- This happens at compat level 140 and 170
- Reproduced SQL Server 2025 > SQL Server 2025
- Reproduced SQL Server 2025 > SQL Server 2019
- Reproduced SQL Server 2019 > SQL Server 2025
The database being queried is managed by an external party so changing the CHECK constraint to remove the table name from the CHECK constraint is possible, but difficult.
I think it could have been caused by a recent patch as I don't remember this issue existing in the past, and it means I can't query the table over linked server easily.
Has anyone seen this before? If anyone uses linked servers with older versions of SQL Server, would you be able to test if this occurs there?
7
u/dbrownems Microsoft Employee 19d ago edited 19d ago
Looks like a bug. Use
as a workaround for now.
Here's a complete repro
3
u/Dry_Author8849 18d ago
Try creating the check constraint without specifying the table name: CHECK (DOB IS NOT NULL)
When using a linked server the table name resolution can be wrongly applied to the table named in the check.
It's a bug anyways. I'm not near a computer right now, so I can't check it myself.
Cheers!
1
u/jshine13371 6 19d ago edited 18d ago
This is a good question and you should post it on DBA.StackExchange.com for a good answer.
Edit: The random downvotes are dumb, when I provided OP a good resource to get help.
2
u/hudequei Microsoft Employee 17d ago
I brought this to the attention of our dev team, we'll be taking a closer look to see the best way to address this.
Thanks for the feedback, folks.