title: Using a CASE Expression in the Default Value
body: <p>I am creating a script for my dba to run. In development I have added a new column to an existing table. When I run the following script I have separated the ALTER and UPDATE on my table. I was trying to combine the statements into one.  Any suggestions? Not sure if this is possible or I am on the right track. Thanks in advance! </p>
  
  <p>Current Script that is working fine:</p>
  
  <pre><code>if not exists(select * from sys.columns where Name = N'NewColumn' and  
  Object_ID = Object_ID(N'TableA'))
  begin
  ALTER TABLE dbo.TableA
  ADD NewColumn BIT NULL
  DEFAULT 0
  end
  
  
  if exists(select * from sys.columns where Name = N'NewColumn' and Object_ID  = Object_ID(N'TableA'))
  begin
  UPDATE dbo.TableA
  SET Newcolumn = CASE WHEN CodeID IN ('A','B') THEN 1 END
  WHERE CodeID IN ('A','B')
  end
  </code></pre>
  
  <p>Trying to combine the ALTER and UPDATE into one:</p>
  
  <pre><code>if not exists(select * from sys.columns where Name = N'NewColumn' and Object_ID = Object_ID(N'TableA'))
  begin
  ALTER TABLE dbo.TableA
  ADD NewColumn BIT NULL
  DEFAULT case CodeID
  WHEN 'A' THEN 1
  WHEN 'B' THEN 1
  end
  </code></pre>
  
  <p>I get the following error:
  The name "CodeID" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.</p>