Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 807
Raha Avatar asked Jul 01 '13 05:07

Raha


People also ask

How do I pass multiple parameters in SQL stored procedure?

Setting up multiple parameters is very easy to do. You just need to list each parameter and the data type separated by a comma as shown below.

Can we pass two output parameter in stored procedure?

A stored procedure can have many output parameters. In addition, the output parameters can be in any valid data type e.g., integer, date, and varying character. Second, after the SELECT statement, we assigned the number of rows returned by the query( @@ROWCOUNT ) to the @product_count parameter.

How do you write a stored procedure with multiple parameters?

The stored procedure with multiple parameters can be created by using the parameter names separated by a comma. Each parameter's data type can be defined along with its name as shown in the example below. As we used LIKE operator, so the query searched any names starting with letter 'J'.

Can pass 3 types of parameters to stored procedures What are they?

As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT.


2 Answers

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

like image 110
Bibek Gautam Avatar answered Sep 28 '22 18:09

Bibek Gautam


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
    
like image 37
Heinzi Avatar answered Sep 28 '22 18:09

Heinzi