Need help in debugging a UDF results

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
LVL 6
bfuchsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcelbaCommented:
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...
0
bfuchsAuthor Commented:
@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
0
bfuchsAuthor Commented:
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
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Scott PletcherSenior DBACommented:
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
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pcelbaCommented:
Great point Scott!

In other words: Each  CASE WHEN ... THEN  command must contain the ELSE part to avoid NULLs.
0
Scott PletcherSenior DBACommented:
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
0
bfuchsAuthor Commented:
Yes, that was indeed the culprit.
Thank you my experts!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.