0

I really would prefer to not rely on sentinel values such as 0, -1, or NULL when testing if an optional parameter to a stored procedure was passed. Is there a definitive way to check?

For example, can I check if any of these sample parameters were specified in the call without assuming they were not passed with a particular value?

CREATE PROCEDURE TestProcedure
     @SampleStringParam VARCHAR(MAX) = NULL
    ,@SampleBooleanParam BIT = 0
    ,@MultiplicativeIdentity REAL = 1
    ,@MultiplicativeInverse REAL = -1
    ,@AdditiveIdentity REAL = 0
AS
...
skink
  • 5,133
  • 6
  • 37
  • 58
Elaskanator
  • 1,135
  • 10
  • 28
  • 2
    No - there's no general way of distinguishing if `@AdditiveIdentity` for example has the value `0` because the default was applied or because that value was passed explicitly. – Martin Smith Oct 23 '17 at 19:39
  • No. You would need mandatory parameters and different procs. – Dan Guzman Oct 23 '17 at 19:39
  • 3
    No, and why would you care? Either way you are going to work with the default value unless another value was specified, that is the point of having default values... – ATC Oct 23 '17 at 20:23

1 Answers1

0
CREATE PROCEDURE TestProcedure
     @SampleStringParam VARCHAR(MAX) = NULL
    ,@SampleBooleanParam BIT = NULL
    ,@MultiplicativeIdentity REAL = NULL
    ,@MultiplicativeInverse REAL = NULL
    ,@AdditiveIdentity REAL = NULL
AS
IF @SampleBooleanParam IS NULL
--  @SampleBooleanParam DEFAULT
SET @SampleBooleanParam = 0
Stanislav Kundii
  • 2,874
  • 1
  • 11
  • 17