Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored Procedure argument "NULL" or "= NULL"

What is the difference between:

CREATE PROCEDURE [dbo].[MyProcedure] 
                @MyArgument INT NULL

and

CREATE PROCEDURE [dbo].[MyProcedure] 
                @MyArgument INT = NULL

I used the first one, and it worked fine in SQL Server 2016. But SQL Server 2012 did not accept it. Both works on SQL Server 2016, and I am using the second one now without problem. But it would be interesting to know the difference.

Thanks!

like image 778
user736570 Avatar asked Dec 20 '19 08:12

user736570


People also ask

How do I check if a stored procedure is null parameter in SQL Server?

Inside the stored procedure, the parameter value is first tested for Null using the ISNULL function and then checked whether it is Blank (Empty). If the parameter has value then only matching records will be returned, while if the parameter is Null or Blank (Empty) then all records from the table will be returned.

Can a parameter accept null values?

In SSRS a multi-value parameter cannot include a NULL value, so users can't filter the data for NULL values. Your requirements state a need to be able to filter the data for NULL values, so in this tip I will demonstrate how to allow NULL values in a multi value SSRS report parameter.

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 a stored procedure parameter have a null value?

There are scenarios when a null value may be passed to a stored procedure parameter. But what if you do not want to accept the null values. In this section, you will learn how you can check if a stored procedure parameter contains a null value or not.

What is the default parameter for stored procedure in SQL Server?

SQL Server Stored Procedure using NULL as Default Parameter. In most cases it is always a good practice to pass in all parameter values, but sometimes it is not possible. So in this example we use the NULL option to allow you to not pass in a parameter value.

How to define input parameters of stored procedures?

Define input parameters of the stored procedures according to the previoulsy created variables. Pass the variables storing the results of the Select statements to the stored procedure input parameters while executing the stored procedure. Let us understand this with an example: Consider the following Customers table.

What is a stored procedure in SQL?

Usually, we use stored procedures to perform an operation on some data. When we call the stored procedure, we pass the data values to the stored procedure through input parameters. The stored procedure takes these input parameters and uses them for completing the operation.


2 Answers

They don't do the same thing. The second one defines a default value for the case that the caller doesn't specify one. The first one doesn't.

The "Transact-SQL Syntax for Natively Compiled Stored Procedures" grammar allows parameter datatypes to be declared as allowing NULL or NOT NULL. This was introduced for Hekaton (memory optimised tables).

Though it isn't documented as supported for the grammar in "Transact-SQL Syntax for Stored Procedures" it looks like it allows NULL but balks at NOT NULL and throws an error.

The parameter '@MyArgument' has been declared as NOT NULL. NOT NULL parameters are only supported with natively compiled modules, except for inline table-valued functions.

There is no value in specifying NULL explicitly - this is the default and only option. There is no declarative syntax for regular stored procs to indicate that parameters must be NOT NULL.

like image 65
Martin Smith Avatar answered Oct 18 '22 02:10

Martin Smith


They do different things:

1) @MyArgument INT NULL - NULL values are allowed for parameter @MyArgument

2) @MyArgument INT = NULL - Default value of parameter @MyArgument is NULL

like image 5
kgzdev Avatar answered Oct 18 '22 03:10

kgzdev