title: How to get sql error in stored procedure
body: <p>I'm using SQL Server 2005. I created a stored procedure which works most of the time, but I found an instance of where it doesn't do what I want. </p>
  
  <p>Currently, the code does something like this</p>
  
  <pre><code>if @@error &lt;&gt; 0
    begin
     select @message_error = "There was a database error adding product "+ @product + " to product line
    end
  </code></pre>
  
  <p>Where <code>@message_error</code> is an output variable.</p>
  
  <p>So, I can <code>select @@error</code> and get a number, but all I really want is the SQL error. </p>
  
  <p>Something like <em>Hey, I couldn't do this because there is a fk constraint on this column</em> or whatever. I found this article on msdn
   <a href="http://msdn.microsoft.com/en-us/library/ms178592(v=sql.90).aspx">http://msdn.microsoft.com/en-us/library/ms178592(v=sql.90).aspx</a></p>
  
  <p>But it only goes over throwing custom exceptions with <code>RAISERROR</code>, I don't want to create my own error message or exception, I just want to know why stuff isn't working. I can execute the stored procedure through Management Studio and see the exact SQL error, but this is tedious trying to match data from the site and manually inserting it that way. </p>
  
  <p>How do I get the SQL error text into an output variable?</p>