'Invalid use of a side-effecting operator 'INSERT'

CREATE FUNCTION insertrecord(@id int, @location varchar(20)) 

RETURNS INT
AS
BEGIN
    INSERT Into [dbo].[Location] (Id,Emploc)values(@id,@location)
    RETURN SCOPE_IDENTITY()
END
GO

Above code gives following Error

Msg 443, Level 16, State 15, Procedure insertrecord, Line 11 Invalid use of a side-effecting operator 'INSERT' within a function.



Solution 1:[1]

The question is how are you planning to use this. But you should have a SP:

CREATE PROCEDURE insertrecord(@id int, @location varchar(20)) 

AS
BEGIN
    INSERT Into [dbo].[Location] (Id,Emploc)values(@id,@location)
    SELECT SCOPE_IDENTITY()
END
GO

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Giannis Paraskevopoulos