Please help me with sql server 2016 syntax for the following If columnX exists then drop columnX
            Asked
            
        
        
            Active
            
        
            Viewed 133 times
        
    -7
            
            
        - 
                    Possible duplicate of [How to check if a column exists in SQL Server table](https://stackoverflow.com/questions/133031/how-to-check-if-a-column-exists-in-sql-server-table) – Thom A Jan 24 '18 at 10:35
1 Answers
1
            
            
        try This
CREATE TABLE T
(
    ID INT,
    VAL INT
)
IF EXISTS(SELECT 1 FROM sys.columns WHERE [object_id] = OBJECT_ID('dbo.T') AND Name = 'VAL')
BEGIN
    ALTER TABLE dbo.T
    DROP COLUMN VAL
END
 
    
    
        Jayasurya Satheesh
        
- 7,826
- 3
- 22
- 39
- 
                    and what about if a column is the primary key, it will just drop the column and will not drop CONSTRAINT – Imran Ali Khan Jan 24 '18 at 10:46
- 
                    When you drop a column the constraints associated with that will be automatically removed – Jayasurya Satheesh Jan 24 '18 at 11:00
