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)
5
  • 1
    As an aside, you might want to review your use of HAVING Commented Jun 6, 2017 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 Commented Jun 6, 2017 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. Commented Jun 6, 2017 at 13:54
  • It's missing a schema name so wont actually run. Commented Jun 6, 2017 at 14:00
  • @AlexK.: schema names are not required for table-valued functions. Commented Jun 6, 2017 at 14:01

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you JohnHC.. It is works fine... Cheers..!
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

1 Comment

I think it is works, if we follow the logic NULLS are stored as an Empty string.
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.