Need help in debugging a UDF results

bfuchs
bfuchs used Ask the Experts™
on
Hi Experts,

I have a function that accepts some parameters and returns a results string.
See attached.

Now I need some help figuring out why the section below only works when the results (@MissingDocs)  includes "Missing: License" as part of the wording, otherwise I dont see it working..

if @BclsExpires between getdate() and getdate()+ 60
	begin
		select @WillExpire = @WillExpire + ', Bcls'
		select @IsBclsDate = 1
	end
if @AclsExpires between getdate() and getdate()+ 60
	begin
		select @WillExpire = @WillExpire + ', Acls'
		select @IsAclsDate = 1
	end
if @NalsExpires between getdate() and getdate()+ 60
	begin
		select @WillExpire = @WillExpire + ', Nals'
		select @IsNalsDate = 1
	end
if @PalsExpires between getdate() and getdate()+ 60
	begin
		select @WillExpire = @WillExpire + ', Pals'
		select @IsPalsDate = 1
	end	

Open in new window

Basically I'm looking for some hints on how to debug this, as I'm using SQL 2005 where I cannot use debuggers tools..
MyFunc.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
To tell why it does not work for you we would need parameter values used for the function call.

You may debug this function simply in SSMS:
Place the whole function to the New query window, remove lines which define the function:
ALTER FUNCTION [dbo].[EmployeesMissingDocs] (
) RETURNS VARCHAR(2000)
AS

and add DECLARE keyword on top of the function parameters. Then simply add parameter values and you may start debugging.

The whole function is a candidate to rewrite. You shouldn't use special parameters for each piece of document/measurement/value but rather process the table where all these values are stored in (hopefully) better structure. To add some new requirement to this function is close to the nightmare...
@pcelba,
and add DECLARE keyword on top of the function parameters. Then simply add parameter values
Can you give me example of how it looks like?
and you may start debugging.
How? I would love to have the ability to step into code line by line (like I have in VBA) however as mentioned my SQL version doesn't support it..
To tell why it does not work for you we would need parameter values used for the function call.
I may get that for you shortly

Thanks,
Ben
Oh I thought would get that from profiler, however I see profiler doesn't provide all details just displays the function that is calling the function is question
see below what I got from there
 SELECT * FROM fn_frmJcahoReportsFrm(562,571) WHERE  Facility1_LastDay >= '1/20/2017' AND ID = 148223 ORDER BY JcahoDueDate 

Open in new window

Any idea how to get all params being sent from my app?

Thanks,
Ben
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
I strongly suspect @WillExpire is getting set to NULL at some point.  For example, code like this:

if @Title = 'ORT'
            begin
                  Select @WillExpire = @WillExpire +
                        case
                              when @LicenseExpires  between getdate() and getdate()+ 60  then ', License'
                              when @LicenseVerDate  between getdate()-365 and getdate()- 335 then ', LicenseVer'
                        end
                  if @LicenseExpiresTwo  between getdate() and getdate()+ 60  select @WillExpire = @WillExpire + ', LicenseTwo'
            end
      if @Title in ('PCT','PCA')
            begin
                  Select @WillExpire = @WillExpire +
                        case
                              when @LicenseExpires  between getdate() and getdate()+ 60  then ', License'
                        end

If neither of the conditions is true, then CASE by default will return NULL.  And NULL concatenated to any value makes the value NULL, and thus returns what looks like an empty string.


To be safe, it's best to use ISNULL throughout all of the code, but for just your specific concern here, you can do it for just the code in that specific condition:

if @BclsExpires between getdate() and getdate()+ 60
      begin
            select @WillExpire = ISNULL(@WillExpire, '') + ', Bcls'
            select @IsBclsDate = 1
      end
if @AclsExpires between getdate() and getdate()+ 60
      begin
            select @WillExpire = ISNULL(@WillExpire, '') + ', Acls'
            select @IsAclsDate = 1
      end
if @NalsExpires between getdate() and getdate()+ 60
      begin
            select @WillExpire = ISNULL(@WillExpire, '') + ', Nals'
            select @IsNalsDate = 1
      end
if @PalsExpires between getdate() and getdate()+ 60
      begin
            select @WillExpire = ISNULL(@WillExpire, '') + ', Pals'
            select @IsPalsDate = 1
      end
Great point Scott!

In other words: Each  CASE WHEN ... THEN  command must contain the ELSE part to avoid NULLs.
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
Yes, if one the WHEN conditions is not 100% known to be true.  One convenient way is to simplay concat an empty string to avoid getting a NULL value instead:

if @Title = 'ORT'
            begin
                  Select @WillExpire = @WillExpire +
                        case
                              when @LicenseExpires  between getdate() and getdate()+ 60  then ', License'
                              when @LicenseVerDate  between getdate()-365 and getdate()- 335 then ', LicenseVer'
                              else ''
                        end
Yes, that was indeed the culprit.
Thank you my experts!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial