Updating some but not all columns

  • Let's say I have a Table with 5 columns.  I'd like to write a SP that allows me to update any or all of the columns on any given call to that SP.  For example, I may need to only update Cols 2 and 4 on a given call.  How can I do this?  I'm thinking I need to send in NULL for the parameters of the Columns I don't want to update, then check for NULL and keep the current value. 

    Thanks,

     

    Brian

  • IF OBJECTPROPERTY(object_id(N'yoursp'), 'IsProcedure') = 1

    DROP PROCEDURE yoursp

    GO

    CREATE PROCEDURE yoursp

    @fld1 int = NULL,

    @fld2 int = NULL,

    @fld3 int = NULL,

    @fld4 int = NULL

    AS

    BEGIN

    UPDATE someTable

    SET fld1 = ISNULL(@fld1, fld1),

    fld2 = ISNULL(@fld2, fld2),

    fld3 = ISNULL(@fld3, fld3)

    fld4 = ISNULL(@fld4, fld4)

    WHERE

    END

  • What if you want to set the value to null?

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

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