title: Only run SQL query if condition met
body: <p>I'm trying to write an 'idiot proof' SQL script that can be run by non-SQL literate users.</p>
  
  <p>My idea is to have a couple of variables defined at the top of the script, then run particular queries based on those variable.</p>
  
  <p>I'm testing on mySQL, but it will eventually run on SQL-Server.</p>
  
  <p>In pseudo-code this is what I'm trying to do:</p>
  
  <pre><code># Set matchThis to the value to match
  SET @matchThis = "matchMe";
  
  # Uncomment (remove the #) one of the two lines below to update or just view
  #SET @update = "YES";
  SET @update = "NO";
  
  IF @update = "YES" {
      UPDATE myTable SET myColumn = "changed" WHERE matchVal = @matchThis;
  } ELSE {
      SELECT * FROM myTable WHERE matchVal = @matchThis;
  }
  </code></pre>
  
  <p>I want to do this entirely in SQL if there is a way.</p>
  
  <p>I've seen guides on using <code>SELECT IF</code> etc but can't figure out how to achieve the above.</p>