title: SQL Server Function/Proc evaluate an expression(s) return an error if not valid
body: <p>I would like to take the code below and create a common function to pass in one or more expressions and to return back an error of my choosing.</p>
  
  <p>Example Code:</p>
  
  <pre><code>IF @Variable1 IS NULL AND @Variable IS NULL or @Variable3 is not null
      BEGIN
  
          -- EITHER DATASET NAME OR ID MUST BE SUPPLIED.
          SET @_msg = 'There was an error'
          SET @_returnValue = -1
          GOTO ERROR_HANDLER
      END
  ERROR_HANDLER: 
  
      -- CREATE THE CLOSING MESSAGE.
      IF @_returnValue &lt;&gt; 0
          RAISERROR(@_msg, 18, 2) WITH SETERROR
  
      RETURN @_returnValue
  </code></pre>
  
  <p>From the above, it would be nice to say something like this below where I could reuse the proc/function and make the code less clutered.</p>
  
  <pre><code>exec ValidateMultipleConditions @Variable1 + 'IS NULL AND ' + @Variable +   'IS NULL or ' + @Variable3 + ' is not null'
  </code></pre>
  
  <p>Anyway, I think with dynamic SQL being passed in this way I could do something where an complete expression could be sent evaluated, validated and then the code continues or stops with an error.</p>
  
  <p>I wanted to see if the community had better ways of doing this or if I'm on the right path.  </p>
  
  <p>Thanks.</p>