Execute a stored procedure in another stored procedure in SQL server

后端 未结 7 1378
盖世英雄少女心
盖世英雄少女心 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:36

    Procedure example:

    Create  PROCEDURE SP_Name
         @UserName nvarchar(200),
         @Password nvarchar(200)
    AS
    BEGIN
        DECLARE  @loginID  int
    
        --Statements for this Store Proc
      --
      -- 
      --
    
      --execute second store procedure
      --below line calling sencond Store Procedure Exec is used for execute Store Procedure.
        **Exec SP_Name_2 @params** (if any) 
    
    
    END
    
    0 讨论(0)
  • 2020-12-01 09:40

    Suppose you have one stored procedure like this

    First stored procedure:

    Create  PROCEDURE LoginId
         @UserName nvarchar(200),
         @Password nvarchar(200)
    AS
    BEGIN
        DECLARE  @loginID  int
    
        SELECT @loginID = LoginId 
        FROM UserLogin 
        WHERE UserName = @UserName AND Password = @Password
    
        return @loginID
    END
    

    Now you want to call this procedure from another stored procedure like as below

    Second stored procedure

    Create  PROCEDURE Emprecord
             @UserName nvarchar(200),
             @Password nvarchar(200),
             @Email nvarchar(200),
             @IsAdmin bit,
             @EmpName nvarchar(200),
             @EmpLastName nvarchar(200),
             @EmpAddress nvarchar(200),
             @EmpContactNo nvarchar(150),
             @EmpCompanyName nvarchar(200)
    
        AS
        BEGIN
            INSERT INTO UserLogin VALUES(@UserName,@Password,@Email,@IsAdmin)
    
            DECLARE @EmpLoginid int
    
            **exec @EmpLoginid= LoginId @UserName,@Password**
    
            INSERT INTO tblEmployee VALUES(@EmpName,@EmpLastName,@EmpAddress,@EmpContactNo,@EmpCompanyName,@EmpLoginid)
        END
    

    As you seen above, we can call one stored procedure from another

    0 讨论(0)
  • 2020-12-01 09:48

    Yes , Its easy to way we call the function inside the store procedure.

    for e.g. create user define Age function and use in select query.

    select dbo.GetRegAge(R.DateOfBirth, r.RegistrationDate) as Age,R.DateOfBirth,r.RegistrationDate from T_Registration R
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 09:52

    Yes, you can do that like this:

    BEGIN
       DECLARE @Results TABLE (Tid INT PRIMARY KEY);
    
       INSERT @Results
    
       EXEC Procedure2 [parameters];
       SET @total 1;
    
    END
    SELECT @total
    
    0 讨论(0)
  • 2020-12-01 09:53

    Your sp_test: Return fullname

    USE [MY_DB]
    GO
    
    IF (OBJECT_ID('[dbo].[sp_test]', 'P') IS NOT NULL)
    DROP PROCEDURE [dbo].sp_test;
    GO
    
    CREATE PROCEDURE [dbo].sp_test 
    @name VARCHAR(20),
    @last_name VARCHAR(30),
    @full_name VARCHAR(50) OUTPUT
    AS
    
    SET @full_name = @name + @last_name;
    
    GO
    

    In your sp_main

    ...
    DECLARE @my_name VARCHAR(20);
    DECLARE @my_last_name VARCHAR(30);
    DECLARE @my_full_name VARCHAR(50);
    ...
    
    EXEC sp_test @my_name, @my_last_name, @my_full_name OUTPUT;
    ...
    
    0 讨论(0)
提交回复
热议问题