Invalid use side-effecting operator Insert within a function

后端 未结 4 1313
栀梦
栀梦 2021-01-07 16:17

I have the following code in my sql function:

if @max_chi > -999
begin
    INSERT INTO CH_TABLE(X1, X2, VALUE)
    VALUES(cur_out.sessionnumber, maxpos, m         


        
4条回答
  •  渐次进展
    2021-01-07 16:57

    You can't use a function to insert data into a base table. Functions return data. This is listed as the very first limitation in the documentation:

    User-defined functions cannot be used to perform actions that modify the database state.

    "Modify the database state" includes changing any data in the database (though a table variable is an obvious exception the OP wouldn't have cared about 3 years ago - this table variable only lives for the duration of the function call and does not affect the underlying tables in any way).

    You should be using a stored procedure, not a function.

提交回复
热议问题