Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does scalar-value function fail with null parameter?
    primarykey
    data
    text
    <p>In TSQL, I need to do a cross-reference from our value for a given field, to the specified client's value. We have many clients and each client's encoding of values is different. The db is a CRM Dynamics 2011 db.</p> <p>I set up a scalar-valued function like this:</p> <pre><code> [dbo].[fn_GetXRef] (@Guid uniqueidentifier, @LookupType nvarchar(20), @OurValue nvarchar(20), @Parm4 nvarchar(20) = null, @Parm5 uniqueidentifier = null, @Parm6 nvarchar(1) = null, @Parm7 nvarchar(1) = null) </code></pre> <p>Parms 4, 5, 6 and 7 may be null; they are used for some cross-references but not others.</p> <p>If I run execute the logic outside the function , it works. When I execute the function it returns NULL.</p> <p>For example:</p> <pre><code>Select dbo.fn_getXRef('22BF20B1-55F1-E211-BF73-00155D062F00', 'Lookup Type 1', 'Our value', null, null, null, '3') </code></pre> <p>returns null but pulling the logic out of the function and running it as a separate query, and using the same input parameter values, returns the correct client-value.</p> <p>What am I not seeing?</p> <hr> <p>update: 12/11/13</p> <p>Thanks all for trying to help. While researching I found some nifty code that looked more effcient than my own so I re-wrote the function using that technique and now it works. It uses OPTION (RECOMPILE):</p> <p>SELECT @TheirValue = X.carriervalue FROM dbo.Filteredcrossreference X WHERE X.carrier = @CarrierId and X.lookuptype = @LookupType and X.ourvalue = @OurValue and (@Parm4 IS NULL OR (X.parm4 = @Parm4)) and (@Parm5 IS NULL OR (X.parm5 = @Parm5)) and (@Parm6 IS NULL OR (X.parm6 = @Parm6)) OPTION (RECOMPILE)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload