I have a TSQL Table-Valued Function and it is complex. I want to ensure that one of the parameters cannot be null. Yet, when I specify NOT NULL after my parameter declaration I am presented with SQL errors.
Is it possible to prevent a parameter of a Table-Valued Function to be assigned null by the calling SQL?
In my opinion, it’d be better to check for NULL values at the beginning of your function and use RAISERROR (no, that’s not a typo) to raise an exception. EDIT: Unfortunately, this doesn’t work for UDFs, so you’ll have to go with option 2.
You also have the option of specifying “RETURNS NULL ON NULL INPUT” when you create your function. If this flag is specified, the function will return NULL if any of its inputs are null…kind of paradoxical, but it may be what you want.
From the MSDN CREATE FUNCTION documentation (quoted because they don’t have an anchor on their page, bleh):
Hope it helps somewhat, and I do agree that it’s needlessly confusing.