How do you raise a SQL Server error event (similar to RAISERROR) from within a SQL CLR routine?
+2
A:
In theory, based on a code sample I got from one of our devs:
try
{
response = service.ProcessThisInfo(info);
}
catch (Exception ex)
{
returnCode = Convert.ToInt32(ErrorEnum.SomeError);
string msg = string.Format("RAISERROR('{0}',16,1)", ex.Message.Replace('\'', ' '));
SqlCommand cmd = new SqlCommand(msg, connection);
try
{
SqlContext.Pipe.ExecuteAndSend(cmd);
}
catch { } // stop duplicate exception throwing...
}
In practise, maybe not It's an issue in sql 2005
gbn
2009-02-04 19:01:44
A:
I think you just get a nasty error if you try this. There is an interesting sample for the SqlContext.Pipe.ExecuteAndSend(cmd) on MSDN at http://msdn.microsoft.com/en-us/library/ms255336(VS.80).aspx It doesn't work without modification, and if you change it to sent a raiserror, it goes dolally and sends you back a NET Exception. You can't do it with a function.
As far as the original question goes, I'd say it (raising a SQL Server error from within a CLR function) was impossible in SQL Server 2005, but can be done from a CLR procedure.
Phil Factor
2009-03-01 21:23:26
Beauchemin and Sullivan, in their book 'A Developers Guide to SQL Server 2005', page 148, show you how to do this in a CLR procedure
Phil Factor
2009-03-02 08:54:48