Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored procedure with default parameters

I am trying to create a stored procedure based on a query I wrote with parameters that are predefined.

When restructuring to a create stored procedure and I execute the stored procedure it states that the parameters were not supplied. Can anyone tell me why?

I know I have missed something essential but after messing about with the code I have reached the point of needing some help from the experts.

This is my code (shortened):

Alter Procedure [Test]     @StartDate AS varchar(6),      @EndDate AS varchar(6) AS     Set @StartDate = '201620' --Define start YearWeek     Set @EndDate  = (SELECT CAST(DATEPART(YEAR,getdate()) AS varchar(4)) + CAST(DATEPART(WEEK,getdate())-1 AS varchar(2)))      SELECT          *     FROM         (SELECT DISTINCT               [YEAR], [WeekOfYear]           FROM               [dbo].[DimDate]           WHERE               [Year] + [WeekOfYear] BETWEEN @StartDate AND @EndDate) dimd     LEFT JOIN          [Schema].[Table1] qad ON (qad.[Year] + qad.[Week of the Year]) = (dimd.[Year] + dimd.WeekOfYear) 

When I run the procedure I get:

Msg 201, Level 16, State 4, Procedure test, Line 0
Procedure or function 'test' expects parameter '@StartDate', which was not supplied.

Thanks in advance.

like image 777
TSQL_Newbie Avatar asked Jul 01 '16 15:07

TSQL_Newbie


People also ask

Can a stored procedure have default values for its parameters?

If you add a parameter when creating a stored procedure, you can provide a default value so that the execution statement is not required to pass input value to this parameter. To provide a default value to a parameter, you should use this format: "@parameter_name data_type = default_value".

What is default parameter type in stored procedure?

The default is an input parameter. To specify an output parameter, the OUTPUT keyword must be specified in the definition of the parameter in the CREATE PROCEDURE statement. The procedure returns the current value of the output parameter to the calling program when the procedure exits.

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.

Can stored procedure have parameters?

A stored procedure can have a maximum of 2100 parameters specified. Each parameter is assigned a name, a data type, and direction like Input, Output, or Return. If a direction is not specified, then by default, it is Input. You can specify a default value for the parameters.


1 Answers

I wrote with parameters that are predefined

They are not "predefined" logically, somewhere inside your code. But as arguments of SP they have no default values and are required. To avoid passing those params explicitly you have to define default values in SP definition:

Alter Procedure [Test]     @StartDate AS varchar(6) = NULL,      @EndDate AS varchar(6) = NULL AS ... 

NULLs or empty strings or something more sensible - up to you. It does not matter since you are overwriting values of those arguments in the first lines of SP.

Now you can call it without passing any arguments e.g. exec dbo.TEST

like image 187
Ivan Starostin Avatar answered Sep 22 '22 15:09

Ivan Starostin