简体   繁体   中英

“Invalid column” error is appearing when running the stored procedure on SSMS but not with SQLCMD

I have a stored procedure mentioned as below. I'm using SQL Server 2008 R2. When I run this stored procedure from SQLCMD it's working and the stored procedure is created, but when I execute the same from SSMS it's failing with the error below.

Msg 207, Level 16, State 1, Procedure TestProc, Line 5 Invalid column name 'col1.

SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER OFF GO

CREATE PROC [dbo].[TestProc]
AS
BEGIN
SELECT t.col1 FROM table t /* But note that col1 is not in table t*/
END
RETURN

How can this problem be fixed?

Is it possible that when you tried to create the stored procedure in SQLCMD, that the table did not exist at the time? Due to the "deferred name resolution" feature of SQL Server, that would allow the stored procedure to be created.

If, at the time you tried to create the stored procedure, the table DID exist, but not the column, then you should be getting the error you are seeing regardless of whether you are using SQLCMD or SSMS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM