10

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

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Raha
  • 163
  • 1
  • 2
  • 12
  • 1
    put parameters /@myDate datetime, /@ServerName sysname – chetan Jul 01 '13 at 05:48
  • 4
    Side note: you should **not** use the `sp_` prefix for your stored procedures. Microsoft has [reserved that prefix for its own use (see *Naming Stored Procedures*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx), and you do run the risk of a name clash sometime in the future. [It's also bad for your stored procedure performance](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix). It's best to just simply avoid `sp_` and use something else as a prefix - or no prefix at all! – marc_s Jul 01 '13 at 05:51
  • 1
    Although your question is a basic one, +1 for adding a complete code example, the complete error message and an explanation of what the code should do. Too many questions lack at least one of these... – Heinzi Jul 01 '13 at 05:51

3 Answers3

10

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
    
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Dear Heinzi, marc_s and Devart thanks for comments. It works now! I know it is a basic question. Hope I will be an expert like all of you. Thanks a lot again. – Raha Jul 01 '13 at 06:16
4

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
Devart
  • 119,203
  • 23
  • 166
  • 186
2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bibek Gautam
  • 581
  • 8
  • 30
  • Here is a misunderstanding. I have declared all parameters in other SP and now I just needed to execute them. Heinzi nd Devart answers' are correct and working. – Raha Jul 04 '13 at 04:33