Anil Sharma

Anil Sharma

  • NA
  • 29
  • 14.2k

Msg 443, Level 16, State 15, Invalid use of a side-effecting

Feb 22 2020 1:52 AM
hi
i m try write a function in sql server there is a error but not under stand. error is
Msg 443, Level 16, State 15, Procedure get_advice_no, Line 21
Invalid use of a side-effecting operator 'UPDATE' within a function.
Msg 443, Level 16, State 15, Procedure get_advice_no, Line 34
Invalid use of a side-effecting operator 'INSERT' within a function.
my function code is
  1. ALTER FUNCTION [dbo].[get_advice_no](@vcounter_type nvarchar(5) )  
  2. RETURNS int  
  3. AS  
  4. BEGIN  
  5. -- Declare the return variable here  
  6. DECLARE @vadvice_no int;  
  7. -- Add the T-SQL statements to compute the return value here  
  8. select @vadvice_no = (SELECT (isnull(CONSTANT_NO,0) + 1) FROM CA_CONSTANT  
  9. WHERE CONSTANT_TYPE = @vcounter_type);  
  10. if @vadvice_no > 0  
  11. begin  
  12. ---if exits(SELECT CONSTANT_NO FROM CA_CONSTANT WHERE CONSTANT_TYPE = @vcounter_type)  
  13. --begin  
  14. update ca_constant  
  15. set constant_no = @vadvice_no  
  16. where CONSTANT_TYPE = @vcounter_type;  
  17. -- end  
  18. end  
  19. else  
  20. begin  
  21. set @vadvice_no = 1;  
  22. IF (@vcounter_type = 'ADV_2')  
  23. begin  
  24. set @vadvice_no = 75001;  
  25. end  
  26. INSERT INTO CA_CONSTANT(CONSTANT_TYPE,CONSTANT_NO)  
  27. VALUES(@vcounter_type,@vadvice_no);  
  28. end  
  29. RETURN(@vadvice_no);  
  30. -- Return the result of the function  
  31. END  
pls tell me what is wrong

Answers (2)