title: Left Outer Join only if column exists (SQL server)
body: <p>I'm using SQL Server Express and I'm trying to pull different columns from different tables using <code>LEFT OUTER JOIN</code>. It's working great but only if all columns exist. So I've been reading for the past hour how to add a condition so that <code>LEFT OUTER JOIN</code> is done only if column exists. </p>
  
  <p>See code below ( the issue is the last <code>LEFT OUTER JOIN</code> because <code>a.[Page Path]</code> doesn't exist):</p>
  
  <pre><code>SELECT 
      b.[Page ID],
      ISNULL(b.[Page Group],'Other Landing Page') AS [Landing Page Group],
      ISNULL(c.[Page Group],'Other Second Page') AS [Second Page Group],
      ISNULL(d.[Page Group],'Other Page') AS [Page Path Group],
      a.*
  
  FROM [mychoice-data-b9BwZvd] a 
  
  
  LEFT OUTER JOIN [mychoice-pagedims] b 
  ON 
     (a.[Landing Page Path] LIKE b.[Page ID])
  
  
  LEFT OUTER JOIN [mychoice-pagedims] c 
  ON 
     (a.[Second Page Path] LIKE c.[Page ID])
  
  
  LEFT OUTER JOIN [mychoice-pagedims] d 
  ON 
     a.[Page Path] LIKE d.[Page ID] 
     WHERE a.[Page Path] IS NOT NULL
  </code></pre>
  
  <p>I've tried <code>IF(EXISTS</code>, but whatever I do I get an error 'Invalid column name 'Page Path''.</p>