title: Error Handling in User Defined Functions
body: <p>I want to write a non-CLR user-defined function in SQL Server 2005. This function takes an input string and returns an output string. If the input string is invalid, then I want to indicate an error to the caller.</p>
  
  <p>My first thought was to use RAISERROR to raise an exception. However, SQL Server does not allow this inside a UDF (though you can raise exceptions in CLR-based UDFs, go figure). </p>
  
  <p>My last resort would be to return a NULL (or some other error-indicator value) from the function if the input value is in error. However, I don't like this option, as it:</p>
  
  <ol>
  <li>Doesn't provide any useful information to the caller</li>
  <li>Doesn't allow me to return a NULL in response to valid input (since it's used as an error code).</li>
  </ol>
  
  <p>Is there any caller-friendly way to halt a function on an error in SQL Server?</p>