Execute a stored procedure in another stored procedure in SQL server

后端 未结 7 1372
盖世英雄少女心
盖世英雄少女心 2020-12-01 09:25

How can i execute a stored procedure in another stored procedure in SQL server? How will I pass the parameters of the second procedure.?

7条回答
  •  有刺的猬
    2020-12-01 09:52

    If you only want to perform some specific operations by your second SP and do not require values back from the SP then simply do:

    Exec secondSPName  @anyparams
    

    Else, if you need values returned by your second SP inside your first one, then create a temporary table variable with equal numbers of columns and with same definition of column return by second SP. Then you can get these values in first SP as:

    Insert into @tep_table
    Exec secondSPName @anyparams
    

    Update:

    To pass parameter to second sp, do this:

    Declare @id ID_Column_datatype 
    Set @id=(Select id from table_1 Where yourconditions)
    
    Exec secondSPName @id
    

    Update 2:

    Suppose your second sp returns Id and Name where type of id is int and name is of varchar(64) type.

    now, if you want to select these values in first sp then create a temporary table variable and insert values into it:

    Declare @tep_table table
    (
      Id int,
      Name varchar(64)
    )
    Insert into @tep_table
    Exec secondSP
    
    Select * From @tep_table
    

    This will return you the values returned by second SP.

    Hope, this clear all your doubts.

提交回复
热议问题