title: Case Statement In Update Query
body: <p>This works in SQL Server, but anytime I try to run it in my powershell environment it gives me an error of incorrect syntax near d.  What must I alter in order to have this run in powershell?</p>
  
  <pre><code>Function Run-Query
  {
      param([string[]]$queries)
  
          $server='Server'
          $dbname='Database'
          $connStr='Server={0};Database={1};Integrated Security=SSPI;' -f $server,$dbname
          For ($i = 0; $i -lt $queries.count; $i++)
          {
              $Command = New-Object System.Data.SqlClient.SqlCommand
              $conn=New-Object System.Data.SqlClient.SQLConnection($connStr)
              $conn.Open()            
              $Command.Connection = $conn
              $Command.CommandText=$queries[$i]
              $Command.ExecuteNonQuery() 
              $conn.Close()
          }
  }
  
  $updatequery = "UPDATE d
                   SET [updatedate] = GETDate(),
                   [shipped] =
                           (
                              CASE WHEN NOT EXISTS(Select 1 *
                                                   from [server].[db].[dbo].[viewname] vw
                                                   WHERE vw.customername = d.customername)                    
                                  THEN 'Yes'
                              ELSE
                                  'PreviousCustomer'
                              END
                           )
                   FROM $fulllocation
                   WHERE ([shipped] IS NULL) 
                   AND ([managerapproved] IS NOT NULL)  
                   AND [readytoship] IN ('Go', 'Ready', 'Send')"
  
  
   Run-Query -queries $updatequery
  </code></pre>