Using UDF in parameter of a stored procedure

  • I didn't think this is allowed but i figured I would ask anyway. I have this:

    declare @STR varchar(max)

    set @STR = fnGetValue()

    exec mySproc @param1 = @STR

    I want to do this:

    exec mySproc @param1 = fnGetValue()

    but this of course will error out with syntax errors....am I correct in assuming I'm stuck with declaring the variable and then passing to the stored procedure or is there syntatically a better way to do this?

  • Change your set to a select, functions requre a select statement.

    declare @STR varchar(max)

    SELECT @STR = dbo.fnGetValue()

    EXEC mySproc @STR

  • yeah thats what i'm doing now (meant to write select instead of set). My question was is it possible to use the function directly in the parameter for the stored procedure instead of having to declare a variable, select the value into the variable and then pass that into the procedure parameter....

  • No, you cannot do that.

  • Parameters work better when you use sp_executeSQL instead of just EXEC. I'm thinking you could do that using sp_ExecuteSQL.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

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

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