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!
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.
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.
As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT.
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.
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.
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.
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.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With