title: SQL case statement in a View
body: <pre><code>ALTER View Myview AS
  
  (SELECT
          TOP 1 ORGANIZATION_id
      FROM ORGANIZATION 
      WHERE code = mo.FunctionCode) org_id,
  (SELECT
          TOP 1 ID
      FROM DEPARTMENT
      WHERE [NAME] = mo.Code) dep_id,
  (SELECT
          TOP 1 Name
      FROM DEPARTMENT
      WHERE [ID] = dep_id) Dep_Name,
  (SELECT
          TOP 1 Org_Name
      FROM CA_ORGANIZATION
      WHERE [organization_id] = org_id) Org_Name,
  (CASE    
      WHEN Dep_Name = 'sales' AND Org_Name = 'Advanture'
          THEN 'salesTeam'
      WHEN Dep_Name ! = 'Sales' AND Org_Name = 'External'
          THEN 'ExternalTeam'
      ELSE
          'DefaultTeam'
      END) type
  
      FROM detail_view dv
      LEFT OUTER JOIN Global_oganization mo  ON  mo.area =dv.code
  </code></pre>
  
  <p>Error while altering the view is</p>
  
  <pre><code>Invalid column name 'Dep_Name'.
  Msg 207, Level 16, State 1, Procedure myview,
  Invalid column name 'Org_Name'.
  Msg 207, Level 16, State 1, Procedure myview,
  Invalid column name 'Dep_Name'.
  Msg 207, Level 16, State 1, Procedure myview,
  Invalid column name 'Org_Name'.
  </code></pre>
  
  <p>I have written CASE statement to validate the department name and organization name to decide the type.</p>
  
  <p>Help me out to fix this error.</p>