SA0260 : Parameter defined as nullable, but no default value provided

Nullable parameters in T-SQL procedures or functions without default values can lead to unexpected runtime errors, as they still require a value during invocation, complicating the execution process.

Description

When defining procedure or function parameters in T-SQL code, it’s common to make some parameters nullable. However, if these nullable parameters do not have a default value specified, they still require a value whenever the procedure or function is called. This can lead to unexpected runtime errors and complicate the invocation of these routines.

For example:

SQL
1-- Example of nullable parameter without default value
2CREATE PROCEDURE SampleProcedure
3    @Parameter1 INT = NULL,
4    @Parameter2 INT -- nullable without default
5AS
6BEGIN
7    SELECT @Parameter1, @Parameter2;
8END;

In this example, @Parameter2 is defined as nullable but without a default value. When calling SampleProcedure, a value must still be provided for @Parameter2, which may not be the intended behavior and can cause errors if overlooked.

  • Causes confusion about parameter requirements, possibly leading to runtime errors.

  • Complicates procedure and function invocation as developers need to provide values even for parameters they expect to default.

`

How to fix

To ensure nullable parameters in stored procedures and functions have a default value, follow these steps:

Follow these steps to address the issue:

  1. Identify nullable parameters in your procedures and functions that do not have a default value.

  2. Modify the parameter definitions to include a default value using the = operator right after the parameter name and data type.

  3. Update your procedures or functions to reflect these changes in their parameter lists to avoid runtime errors and simplify usage.

For example:

SQL
1-- Corrected example with default value for nullable parameter
2CREATE PROCEDURE SampleProcedure
3    @Parameter1 INT = NULL,
4    @Parameter2 INT = NULL -- Now includes a default value
5AS
6BEGIN
7    SELECT @Parameter1, @Parameter2;
8END;

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
3 minutes per issue.
Categories

Design Rules, Bugs

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 1CREATE PROCEDURE SA00256.TestProc
 2  @param1 int 
 3, @param2 int NULL
 4, @param3 int NULL = NULL
 5, @param4 int = NULL
 6AS
 7BEGIN
 8    SET NOCOUNT ON;
 9        /* PROCEDURE BODY */
10END

Analysis Results
  Message Line Column
1 SA0260 : Parameter defined as nullable, but no default value provided. 3 2
See Also

Other Resources