SQL Server - Invalid Use of a Side-Effecting Operator 'insert' within a Function

Introduction:

Here I will explain how to solve the problem of “invalid use of a side-effecting operator 'insert' within a function in SQL Server”.

Description:

In previous posts I explained many articles relating to solve errors in asp.net, SQL Server, IIS, etc. Now I will explain how to solve the problem of “invalid use of a side-effecting operator 'insert' within a function in SQL Server”.

Generally this problem will occur whenever we try to write insert, update and delete queries inside of SQL Server functions like as shown below
CREATE FUNCTION [dbo].[testfunction]
(
@LeadChannel VARCHAR(250),
@UserId INT
)
RETURNS INT
AS
BEGIN
DECLARE @Channel INT
Set @Channel  = 0
INSERT INTO PRESALE_LeadChannel(Source,Createdby)
VALUES(@LeadChannel,@UserId)
SELECT @Channel= @@IDENTITY
RETURN @Channel
END
In above function I written insert query because of that when we call this function we will get error like invalid use of a side-effecting operator 'insert' within a function.
To solve this problem we should not use insert, update and delete quries inside of functions.
I hope it helps you to solve your problem. Happy Coding………

Comments