I'm writing a user-defined function in SQL Server 2008. I know that functions cannot raise errors in the usual way - if you try to include the RAISERROR statement SQL returns:
Msg 443, Level 16, State 14, Procedure ..., Line ...
Invalid use of a side-effecting operator 'RAISERROR' within a function.
You can use CAST to throw meaningful error:
create function dbo.throwError() returns nvarchar(max) as begin return cast('Error happened here.' as int); end
Then Sql Server will show some help information:
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value 'Error happened here.' to data type int.