Invalid use side-effecting operator Insert within a function

后端 未结 4 1320
栀梦
栀梦 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:31

    Disclaimer: This is not a solution, it is more of a hack to test out something. User-defined functions cannot be used to perform actions that modify the database state.

    I found one way to make insert or update using sqlcmd.exe so you need just to replace the code inside @sql variable.

    CREATE FUNCTION [dbo].[_tmp_func](@orderID NVARCHAR(50))
    RETURNS INT
    AS
    BEGIN
    DECLARE @sql varchar(4000), @cmd varchar(4000)
    SELECT @sql = 'INSERT INTO _ord (ord_Code) VALUES (''' + @orderID + ''') '
    SELECT @cmd = 'sqlcmd -S ' + @@servername +
                  ' -d ' + db_name() + ' -Q "' + @sql + '"'
    EXEC master..xp_cmdshell @cmd, 'no_output'
    RETURN 1
    END
    

提交回复
热议问题