How to call procedure from function. Getting error Invalid use of side-effecting or time-dependent operator in 'INSERT EXEC' within a function.

  • Hi!

    I am trying to call procedure from function but getting following error.

    Invalid use of side-effecting or time-dependent operator in 'INSERT EXEC' within a function.

    Actually I want table valued function so that I can use it in outer apply to process every left input.

    and in that function I am calling another already available procedure.

    may be this is not valid here but another alternative I can see is use of cursor which I have to prevent because of possible slowness of query.

    Can anybody suggest some workaround?

  • mote.ajit2 (12/4/2013)


    Hi!

    I am trying to call procedure from function but getting following error.

    Invalid use of side-effecting or time-dependent operator in 'INSERT EXEC' within a function.

    Actually I want table valued function so that I can use it in outer apply to process every left input.

    and in that function I am calling another already available procedure.

    may be this is not valid here but another alternative I can see is use of cursor which I have to prevent because of possible slowness of query.

    Can anybody suggest some workaround?

    you'll most likely have to change your function into a procedure instead. functions have a number of rules that have to be followed; dynamic SQL is one of the items not allowed, as well as others. if you show us your function or better explain what you are trying to do, we can help better.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • There is a workaround in which you can use OPENROWSET to call a stored procedure from a function; however, this is in general a bad idea and should be avoided.

  • INSERT EXEC is not the best method by itself.

    I would try to extract the code from your SP and wrap it in new function, then change the SP to call this function, and in the function you are working on you could just call that other function.

    This way you wouldn't duplicate the code, the applications, calling your SP wouldn't be affected and you wouldn't have to use INAERT EXEC.

    Of course this method works only if your SP doesn't use dynamic SQL and other things, not allowed to use within a function 🙂

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply