How to call Stored Procedures (with 2 parameters) in a Stored Procedure?

[亡魂溺海] 提交于 2019-12-06 20:04:26

问题


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).

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".


回答1:


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. 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).

    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:


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:


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..



来源:https://stackoverflow.com/questions/17398082/how-to-call-stored-procedures-with-2-parameters-in-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!