Nulls

  • I have a stored procedure where you can pass either @tableId, or @ParentId. In this case the programmer is passing in @tableId and the parentId is null.

    Variable

    DELETE FROM [dbo].

    WHERE

    [tableID] = ISNULL(@tableID,[tableID])

    AND parentId = ISNULL(@parentId,parentId)

    Data

    DELETE FROM [dbo].

    WHERE

    [tableID] = ISNULL('XXXX-XXXX-XXXX',[tableID])

    AND parentId = ISNULL(NULL,parentId)

    So my problem is parentid allows Nulls. this table has two parents so it has to allow nulls. ParentId is null in the table so it looks like Null = Null. It seems that the SP completely throws out the command. Is there something I can do to fix this response? These are all guids

  • This is how I normally do it. I would probably do some additional checks on the data before performing a delete of this nature, but the concept is the same.

    [font="Courier New"]IF COALESCE(@ParentID, @TableID) IS NULL RETURN -- Exit if no parameter was passed

    IF @ParentID IS NULL -- Update Based on TableID

            DELETE FROM [dbo].

            WHERE [tableID] = @tableID

    ELSE -- Update Based on ParentID

       DELETE FROM [dbo].

            WHERE [parentID] = @parentID

    [/font]

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

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