Skip to content

[Bug]: IntelliSense falsely reports "Invalid column name" after column is added in same script #19632

Open
@SoumyadipYT-OSS

Description

@SoumyadipYT-OSS

Description

When executing a SQL script that drops a column and adds it back, the VS Code MSSQL extension incorrectly reports "Invalid column name" errors for subsequent operations on that column, even though the operations execute successfully on the server. The IntelliSense/validation appears to use outdated metadata and doesn't refresh after schema changes within the same script.

Steps to Reproduce

  1. Create a SQL script that drops a column from a table
  2. Add proper GO statements to separate batches
  3. In the same script, add the column back to the table
  4. Try to reference the column in subsequent operations (UPDATE, ALTER, etc.)
  5. Observe that VS Code shows squiggly red lines under the column name with "Invalid column name" errors
  6. Execute the script and notice it runs successfully despite the reported errors

`
-- Drop the column if it exists
IF EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Inventory.Furniture') AND name = 'Subcategory')
BEGIN
ALTER TABLE Inventory.Furniture DROP COLUMN Subcategory;
END
GO

-- Add the column back
ALTER TABLE Inventory.Furniture
ADD Subcategory VARCHAR(50) NULL;
GO

-- VS Code reports "Invalid column name 'Subcategory'" on this line despite the column existing
UPDATE Inventory.Furniture SET Subcategory = 'General' WHERE Subcategory IS NULL;
GO
`

I have my own code:

USE PracticeDatabase;
GO 

-- First, check if the column exists and if there are any constraints
SELECT 
    c.name AS column_name,
    dc.name AS constraint_name
FROM 
    sys.columns c
LEFT JOIN
    sys.default_constraints dc ON c.default_object_id = dc.object_id
WHERE 
    c.object_id = OBJECT_ID('Inventory.Furniture') AND
    c.name = 'Subcategory';
GO

-- Conditionally drop constraint if it exists
-- We'll use dynamic SQL to handle this
DECLARE @constraintName NVARCHAR(128);
SELECT @constraintName = dc.name
FROM sys.columns c
JOIN sys.default_constraints dc ON c.default_object_id = dc.object_id
WHERE c.object_id = OBJECT_ID('Inventory.Furniture')
AND c.name = 'Subcategory';

IF @constraintName IS NOT NULL
BEGIN
    DECLARE @sql NVARCHAR(200) = N'ALTER TABLE Inventory.Furniture DROP CONSTRAINT ' + QUOTENAME(@constraintName) + ';';
    EXEC sp_executesql @sql;
    PRINT 'Constraint dropped: ' + @constraintName;
END
ELSE
BEGIN
    PRINT 'No constraint found for Subcategory column.';
END
GO

-- Now it's safe to drop the column (if it exists)
IF EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Inventory.Furniture') AND name = 'Subcategory')
BEGIN
    ALTER TABLE Inventory.Furniture DROP COLUMN Subcategory;
    PRINT 'Column Subcategory dropped.';
END
ELSE
BEGIN
    PRINT 'Column Subcategory does not exist.';
END
GO

-- Verify the change
SELECT * FROM Inventory.Furniture;
GO

-- Add the Subcategory column as nullable
ALTER TABLE Inventory.Furniture
ADD Subcategory VARCHAR(50) NULL;
GO

-- Verify the column was added
SELECT * FROM Inventory.Furniture;
PRINT 'Column Subcategory added successfully.';
GO





-- Update the Subcategory column as Default that is NOT NULL

-- Step 1: First update all existing NULL values to a default value
UPDATE Inventory.Furniture SET Subcategory = 'General' WHERE Subcategory IS NULL;
GO

-- Step 2: Add a default constraint for new rows
ALTER TABLE Inventory.Furniture ADD CONSTRAINT DF_Furniture_Subcategory DEFAULT 'General' FOR Subcategory;
GO

-- Step 3: Alter the column to be NOT NULL
ALTER TABLE Inventory.Furniture ALTER COLUMN Subcategory VARCHAR(50) NOT NULL;
GO

-- Verify the changes
SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_SCHEMA = 'Inventory' AND 
    TABLE_NAME = 'Furniture' AND
    COLUMN_NAME = 'Subcategory';
GO

-- Test with a quick select
SELECT * FROM Inventory.Furniture;
GO

Unable to detect Subcategory column, though it is there in the Table, Mostly Important: Query runs properly and showing the output in QUERY RESULTS

Affected Area

  • Connection dialog
  • Query results panel
  • Query editor
  • Object Explorer
  • Table Designer
  • Schema Compare
  • Schema Designer
  • Local container
  • GitHub Copilot integration
  • Query Plan Visualizer
  • Other (please describe below)

If you selected "Other", please describe the affected area

• IntelliSense/code validation
• Metadata caching
• SQL script execution

Environment Information

• VS Code Version: 1.101.1 (user setup)
• MSSQL Version: 1.33.0
• Windows Version: 11, 24H2
• SQL Server Version: Microsoft SQL Server 2022 (RTM-GDR) (KB5046861) - 16.0.1135.2 (X64)

Confirmation

  • I have searched existing issues and couldn't find a match
  • I want to work on this issue

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions