问题
I have a SQL Server stored procedure that updates a large table. Because of its size, most parameters are optional. Most recommendations for setting the default of my optional parameters is to set them to NULL.
However, my problem is that NULL is a legitimate update parameter value so I'm trying to figure out how to handle detect the difference between a NULL that is passed vs. a NULL that exists due to a missing optional parameter.
One solution I've seen is to set the optional parameter's default value to something odd that will never be passed in real life and then check for that later in the SP. This has the advantage of not requiring the user to do anything and all the logic is self-contained.
Another solution is to force the user to pass a flag to indicate null, for example, the string "NULL" vs. the value null. While this makes the SP code more standard, it forces the user to know more about the SP than is usually required. Unfortunately, this would only work for VARCHAR datatypes. I also have to figure this out for INTs and DateTime fields.
What would be a good solution?
BTW, this is an updated question that originated here
来源:https://stackoverflow.com/questions/64945812/how-to-pass-null-to-a-optional-parameter-and-it-not-be-interpreted-as-a-missing