如何在存储过程中调用存储过程(带有2个参数)?

时间:2022-04-12 16:37:46

I have stored procedures with same parameters (server name and date). I want to write a stored procedure and Exec them in that SP (called it SP_All).

我有相同参数(服务器名称和日期)的存储过程。我想编写一个存储过程并在该SP中执行它们(称为SP_All)。

CREATE PROCEDURE [dbo].[SP_All]
AS
BEGIN
exec sp_1   @myDate datetime, @ServerName sysname
exec sp_2   @myDate datetime, @ServerName sysname
exec sp_3   @myDate datetime, @ServerName sysname
exec sp_4   @myDate datetime, @ServerName sysname
END
Go 

error: Must declare the scalar variable "@myDate".

错误:必须声明标量变量“@myDate”。

3 个解决方案

#1


7  

I see two issues here:

我在这里看到两个问题:

  1. Your procedure apparently takes two parameters, @myDate and @ServerName, which you have not declared yet. Do so by adding the names and the types between the procedure name and AS.
  2. 您的过程显然需要两个参数,@ myDate和@ServerName,您尚未声明。通过在过程名称和AS之间添加名称和类型来执行此操作。
  3. When calling sp_1 to sp_4, there is no need to specify the data type of the parameters again (that's been taken care of by the declaration, see point 1).

    在将sp_1调用到sp_4时,无需再次指定参数的数据类型(已由声明处理,请参见第1点)。

    CREATE PROCEDURE [dbo].[SP_All]
        @myDate datetime,
        @ServerName sysname
    AS
    BEGIN
        exec sp_1 @myDate, @ServerName
        exec sp_2 @myDate, @ServerName
        exec sp_3 @myDate, @ServerName
        exec sp_4 @myDate, @ServerName
    END
    

#2


3  

Try this one -

试试这个 -

CREATE PROCEDURE [dbo].[SP_All]

       @myDate DATETIME
     , @ServerName SYSNAME

AS BEGIN

     EXEC dbo.sp_1 @myDate, @ServerName
     EXEC dbo.sp_2 @myDate, @ServerName
     EXEC dbo.sp_3 @myDate, @ServerName
     EXEC dbo.sp_4 @myDate, @ServerName

END

#3


1  

You are executing stored procedures the wrong way

您正在以错误的方式执行存储过程

exec sp_1 @myDate datetime, @ServerName sysname

is completely wrong syntax.

是完全错误的语法。

When you have to execute a stored procedure with parameters, first declare parameter and pass it..

当您必须使用参数执行存储过程时,首先声明参数并传递它。

declare @myDate datetime
declare @ServerName sysname

exec sp_1 @myDate, @ServerName

This is the right approach..

这是正确的方法..

#1


7  

I see two issues here:

我在这里看到两个问题:

  1. Your procedure apparently takes two parameters, @myDate and @ServerName, which you have not declared yet. Do so by adding the names and the types between the procedure name and AS.
  2. 您的过程显然需要两个参数,@ myDate和@ServerName,您尚未声明。通过在过程名称和AS之间添加名称和类型来执行此操作。
  3. When calling sp_1 to sp_4, there is no need to specify the data type of the parameters again (that's been taken care of by the declaration, see point 1).

    在将sp_1调用到sp_4时,无需再次指定参数的数据类型(已由声明处理,请参见第1点)。

    CREATE PROCEDURE [dbo].[SP_All]
        @myDate datetime,
        @ServerName sysname
    AS
    BEGIN
        exec sp_1 @myDate, @ServerName
        exec sp_2 @myDate, @ServerName
        exec sp_3 @myDate, @ServerName
        exec sp_4 @myDate, @ServerName
    END
    

#2


3  

Try this one -

试试这个 -

CREATE PROCEDURE [dbo].[SP_All]

       @myDate DATETIME
     , @ServerName SYSNAME

AS BEGIN

     EXEC dbo.sp_1 @myDate, @ServerName
     EXEC dbo.sp_2 @myDate, @ServerName
     EXEC dbo.sp_3 @myDate, @ServerName
     EXEC dbo.sp_4 @myDate, @ServerName

END

#3


1  

You are executing stored procedures the wrong way

您正在以错误的方式执行存储过程

exec sp_1 @myDate datetime, @ServerName sysname

is completely wrong syntax.

是完全错误的语法。

When you have to execute a stored procedure with parameters, first declare parameter and pass it..

当您必须使用参数执行存储过程时,首先声明参数并传递它。

declare @myDate datetime
declare @ServerName sysname

exec sp_1 @myDate, @ServerName

This is the right approach..

这是正确的方法..