Sending a Null Value in a Store Proc

  • Newbie question here. But...if I don't ask, I dont learn.

    Writing and testing a new SP which will insert data to a table. While testing, I'm trying to essentially send NULL (is that even possible) since the field accepts Null values. For example....

    EXEC sp_InsertCustomer

    @strFirstName = 'Peter',

    @strLastName = 'Parker',

    @strAddress1 = '1000 Street',

    @strAddress2 = '#500',

    @strAddress3 = '',

    @strCity

    ...etc...

    The problem is @strAddress3 is sending an empty string. I do not want that. I want nothig/Null to go into that field.

    I've tried various versions of this:

    @strAddress3 = Null,

    @strAddress3 Null,

    @strAddress3 Is Null,

    ..etc...but doesn't work.

    If address3 is truly Null, should I just omit this declaration and one line of code? Or is there way to test using a Null?

  • RedBirdOBX (5/23/2013)


    Newbie question here. But...if I don't ask, I dont learn.

    Writing and testing a new SP which will insert data to a table. While testing, I'm trying to essentially send NULL (is that even possible) since the field accepts Null values. For example....

    EXEC sp_InsertCustomer

    @strFirstName = 'Peter',

    @strLastName = 'Parker',

    @strAddress1 = '1000 Street',

    @strAddress2 = '#500',

    @strAddress3 = '',

    @strCity

    ...etc...

    The problem is @strAddress3 is sending an empty string. I do not want that. I want nothig/Null to go into that field.

    I've tried various versions of this:

    @strAddress3 = Null,

    @strAddress3 Null,

    @strAddress3 Is Null,

    ..etc...but doesn't work.

    If address3 is truly Null, should I just omit this declaration and one line of code? Or is there way to test using a Null?

    There is more than 1 way to handle this. If an empty string is what you consider to be NULL then you could do a couple of things. You could add some code in the calling program to send NULL if the value is ''.

    You could alter the store proc declaration so that it has a default of NULL for @strAddress3 and not pass that value.

    i.e.:

    create proc

    ...

    @strAddress3 = NULL,

    ...

    Or you could alter your proc and have it check for ''.

    create proc

    ..

    AS

    if @strAddress3 = ''

    set @strAddress3 = NULL

    Does this help?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (5/23/2013)


    create proc

    ..

    AS

    if @strAddress3 = ''

    set @strAddress3 = NULL

    [/code]

    Or just...

    SELECT @strAddress3 = NULLIF(strAddress3,'')

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Yes, you can pass a NULL into a stored proc.

    Try this:

    create proc testNullParam @p1 varchar(10),@p2 varchar(10)=null,@p3 varchar(10)

    as

    select @p1 as p1, @p2 as p2, @p3 as p3

    go

    exec testNullParam @p1='test',@p2=null,@p3='test'

    You get this:

    +--------------------+

    ¦[highlight="#808080"] p1 [/highlight]¦[highlight="#808080"] p2 [/highlight]¦[highlight="#808080"] p3 [/highlight]¦

    +------+------+------¦

    ¦ test ¦ NULL ¦ test ¦

    +--------------------+

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply