I am trying to write a DML script that updates a column, but I wanted to make sure the column was the first, so I wrapped it in an IF EXISTS block
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='Client' AND COLUMN_NAME='IsClarityEnabled') BEGIN UPDATE Client SET IsClarityEnabled = 1 WHERE ClientID = 21 END
So, the oddity is that it tries to perform the update, even if it does not fulfill the condition. So the column does not exist and the UPDATE statement works, and I get an error. Why?
Even a stranger is that this works:
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='Client' AND COLUMN_NAME='IsClarityEnabled') BEGIN EXEC('UPDATE Client SET IsClarityEnabled = 1 WHERE ClientID = 21') END
Is there anything special about the UPDATE command that forces it to behave this way?
source share