Invalid column name “USER_SOURCE”

五迷三道 提交于 2019-12-02 00:06:52

Here's a script which I think reproduces a problem identical to yours:

CREATE PROCEDURE TestTmpTable
  @value varchar(20)
AS
BEGIN
  CREATE TABLE #test (id int IDENTITY, value varchar(20));
  INSERT INTO #test (value) VALUES (@value)
  SELECT * FROM #test;
  DROP TABLE #test;
END

GO

CREATE TABLE #test (id int IDENTITY, value2 varchar(20));
EXEC TestTmpTable 'some text';
SELECT * FROM #test;
DROP TABLE #test;

GO

DROP PROCEDURE TestTmpTable

As you can see, there are two #test tables here, one is created in the stored procedure, the other one in the batch that invokes the stored procedure. They have different structures: one has a column named value, the other a column named value2. If you run the script, you'll see this error:

Msg 207, Level 16, State 1, Procedure TestTmpTable, Line 6
Invalid column name 'value'.

I can't point you to a relevant documentation article at the moment, but to me it is evident enough that some preliminary name checking is taking place immediately before the execution of the SP. At that stage, a discrepancy between the column names referenced in the stored procedure and those actually present in the already existing table is revealed, which renders the execution impossible.

If you change value2 to value, the script will work without any problem, and there will be two row sets in the output, one with the 'some text' value, the other empty. And of course the script will work if you remove all parts related to the external #test table.

So, check the places where your procedure is called to see if any other #users table can be existing by that moment, and if so, amend the issue according to your situation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!