Cannot use Raiserror in a UDF?
Posted on 2006-04-06
I get the following error when I attempt to raise an error in a scalar UDF, SQL Server 2005.
Msg 443, Level 16, State 14, Procedure sample_of_the_problem, Line 6
Invalid use of side-effecting or time-dependent operator in 'RAISERROR' within a function.
The documentation doesn't explicity say anything about raising an error. If anyone has a definitive answer about whether or not this is ever supported, let me know.
create function sample_of_the_problem(
) returns integer
if @ID is null begin
Works fine if if the raiserror is commented-out. Returns same error number with a slightly different message (Invalid use of 'RAISEERROR' within a function) in SQL Server 2000.
I tried to get tricky and call a stored procedure that raises the error, but that's not permitted either. Interestingly, this error occurs during the CALL to the function, not when the function is created.
Msg 557, Level 16, State 2, Line 1
Only functions and extended stored procedures can be executed from within a function.
So I'm left with using some kind of signaling value to indicate error conditions.