0

My UDF's body has the following HAVING class.

HAVING   
         Company.Description            = @Company       AND
         SystemCustomerType.Description = @CustomerType

I tried to call this by this following syntax.

SELECT * FROM FunctionName('ABC_Company',NULL)

And also tried to set default value as NULL for @CustomerType parameter. And called the function by

SELECT * FROM FunctionName('ABC_Company',default)
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • 1
    As an aside, you might want to review your use of `HAVING` – JohnHC Jun 06 '17 at 13:50
  • 2
    Check for HAVING cluase... normally having clause used to filter on aggregation for filtering on column you might require to use WHERE itself – Kannan Kandasamy Jun 06 '17 at 13:52
  • And your problem is? This should work fine on the syntax level, but as always, you can't compare `NULL` values using `=`; the function itself would have to accommodate for it using `IS NULL`, `ISNULL` or [some other technique](https://stackoverflow.com/questions/1075142/how-to-compare-values-which-may-both-be-null-is-t-sql). – Jeroen Mostert Jun 06 '17 at 13:54
  • It's missing a schema name so wont actually run. – Alex K. Jun 06 '17 at 14:00
  • @AlexK.: schema names are not required for table-valued functions. – Jeroen Mostert Jun 06 '17 at 14:01

3 Answers3

3

Try this:

where (Company.Description = @Company or @Company is null)
and (SystemCustomerType.Description = @CustomerType or @CustomerType is null)

Then use:

select * from FunctionName('ABC_Company',NULL)
JohnHC
  • 10,935
  • 1
  • 24
  • 40
0

you can use NULL as parameter and then:

HAVING   
         Company.Description            = @Company       AND
         SystemCustomerType.Description = isnull(@CustomerType,'')

provided you don't need to differenciate between NULL and empty string

Jayvee
  • 10,670
  • 3
  • 29
  • 40
0

'something' = NULL always returns false. To check if something is NULL use 'is'.

HAVING   
     Company.Description            = @Company       AND
     (SystemCustomerType.Description = @CustomerType 
     or (SystemCustomerType.Description is null and @CustomerType  is null))
Peter
  • 850
  • 5
  • 16