How to pass NULL to a optional parameter and it not be interpreted as a missing parameter

岁酱吖の 提交于 2021-01-07 02:47:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!