Results 1 to 4 of 4

Thread: Conditional Updates

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Resolved Conditional Updates

    In my stored procedure I have a large amount of data that can be updated. What I would like to do is not update anything that is passed into the stored procedure as NULL. How can I accomplish this?

    Basically I'm looking for something like this:
    Code:
    UPDATE [MySchema].[MyTable]
    	SET
    	IF (@MyField IS NOT NULL)
    	BEGIN
    		MyField  = @ MyField 
    	END
    	WHERE @UserID = UserID
    Last edited by Kasracer; Sep 27th, 2008 at 10:57 AM.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Conditional Updates

    Not sure I've got the syntax quite right, but here's one method:
    Code:
    	SET MyField  =
      	CASE WHEN (@MyField IS NULL) Then
    		MyField
    	ELSE	
    		@MyField 
    	END

  3. #3

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Conditional Updates

    That works perfectly. Why didn't I think of that? Thanks!
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Conditional Updates

    Actually there is a simpler way - just use ISNULL in reverse

    Code:
    SET MyField  = IsNull(@MyField,MyField)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width