Why does Microsoft SQL Server check columns but not tables in stored procs? Why does Microsoft SQL Server check columns but not tables in stored procs? sql sql

Why does Microsoft SQL Server check columns but not tables in stored procs?


This is called deferred name resolution.

There is no way of turning it off. You can use dynamic SQL or (a nasty hack!) add a reference to a non existent table so that compilation of that statement is deferred.

CREATE PROCEDURE [dbo].[MyProcedure]ASBEGINCREATE TABLE #Dummy (c int)    SELECT        NonExistantCol1, NonExistantCol2, NonExistantCol3    FROM        ExistantTable     WHERE NOT EXISTS(SELECT * FROM #Dummy)    DROP TABLE #DummyENDGO


This article in MSDN should answer your question.

From the article:

When a stored procedure is executed for the first time, the query processor reads the text of the stored procedure from the sys.sql_modules catalog view and checks that the names of the objects used by the procedure are present. This process is called deferred name resolution because table objects referenced by the stored procedure need not exist when the stored procedure is created, but only when it is executed.