SQL Server: Invalid Column Name

前端 未结 13 882
一个人的身影
一个人的身影 2020-12-22 23:49

I am working on modifying the existing SQL Server Stored Procedure. I added two new columns to the table and modified the stored procedure as well to select these two column

相关标签:
13条回答
  • 2020-12-23 00:09

    I was getting the same error when creating a view.

    Imagine a select query that executes without issue:

    select id
    from products
    

    Attempting to create a view from the same query would produce an error:

    create view app.foobar as
    select id
    from products
    

    Msg 207, Level 16, State 1, Procedure foobar, Line 2
    Invalid column name 'id'.

    For me it turned out to be a scoping issue; note the view is being created in a different schema. Specifying the schema of the products table solved the issue. Ie.. using dbo.products instead of just products.

    0 讨论(0)
  • 2020-12-23 00:13

    Could also happen if putting string in double quotes instead of single.

    0 讨论(0)
  • 2020-12-23 00:13

    This error may ALSO occur in encapsulated SQL statements e.g.

    DECLARE @tableName nvarchar(20) SET @tableName = 'GROC'

    DECLARE @updtStmt nvarchar(4000)

    SET @updtStmt = 'Update tbProductMaster_' +@tableName +' SET department_str = ' + @tableName exec sp_executesql @updtStmt

    Only to discover that there are missing quotations to encapsulate the parameter "@tableName" further like the following:

    SET @updtStmt = 'Update tbProductMaster_' +@tableName +' SET department_str = ''' + @tableName + ''' '

    Thanks

    0 讨论(0)
  • 2020-12-23 00:16

    I had a similar problem.

    Issue was there was a trigger on the table that would write changes to an audit log table. Columns were missing in audit log table.

    0 讨论(0)
  • 2020-12-23 00:18

    If you are going to ALTER Table column and immediate UPDATE the table including the new column in the same script. Make sure that use GO command to after line of code of alter table as below.

    ALTER TABLE Location 
    ADD TransitionType SMALLINT NULL
    GO   
    
    UPDATE Location SET TransitionType =  4
    
    ALTER TABLE Location 
        ALTER COLUMN TransitionType SMALLINT NOT NULL
    
    0 讨论(0)
  • 2020-12-23 00:19

    Following procedure helped me solve this issue but i don't know why.

    1. Cut the code in question given by the lines in the message
    2. Save the query (e.g. to file)
    3. Paste the code to where it was before
    4. Again save the query

    Even if it seems to be the same query executing it did not throw this error

    0 讨论(0)
提交回复
热议问题