Pass Stored Proc OUTPUT into INSERT

  • From an SP, i wuold be getting the result which i need to pass as one of the values to an insert statement.

    insert into table (@value1, @value2)

    @value - declared and set

    @value2 - result of stored procedure

  • balasach82 (1/24/2015)


    From an SP, i wuold be getting the result which i need to pass as one of the values to an insert statement.

    insert into table (@value1, @value2)

    @value - declared and set

    @value2 - result of stored procedure

    There are several ways you can do this depending upon your stored procedures and what exactly you want inserted.

    From your example it looks like this is a scalar value, so you might use an output parameter in the stored procedure, call the procedure passing it @value2 as the output parameter, then doing your insert. These are 2 separate SQL statements.

    EXEC mySP(parameters, @value2);

    insert into table (@value1, @value2);

    If you want the stored procedure to be called as part of a select statement itself, you may actually want to write it as a stored function instead.

    insert into table (@value1, myFunction(parameters));

    These are by no means the ONLY way of accomplishing the task. It depends how you need to call the procedure or function.

  • Your question is a bit obscure, not really sure what you are attempting to accomplish. If you are trying to insert the result set returned by a stored procedure you can do the following. It does require that the table be declared first.

    insert into dbo.MyTable(col1, col2, ...)

    exec sp_myproc parm1, parm2, ...

    Please note that the parameters to the stored procedure have no relation to the columns in the target table, in fact your stored procedure may not have any parameters.

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

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