-7

Please help me with sql server 2016 syntax for the following If columnX exists then drop columnX

user2447136
  • 179
  • 3
  • 4
  • 16
  • 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 Answers1

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