views:

33

answers:

2

I'm trying to figure out if this is relatively well-performing T-SQL (this is SQL Server 2008). I need to create a stored procedure that updates a table. The proc accepts as many parameters as there are columns in the table, and with the exception of the PK column, they all default to NULL. The body of the procedure looks like this:

CREATE PROCEDURE proc_repo_update 
    @object_id bigint
    ,@object_name varchar(50) = NULL
    ,@object_type char(2) = NULL
    ,@object_weight int = NULL
    ,@owner_id int = NULL
    -- ...etc
AS
BEGIN
    update
        object_repo
    set
        object_name = ISNULL(@object_name, object_name)
        ,object_type = ISNULL(@object_type, object_type)
        ,object_weight = ISNULL(@object_weight, object_weight)
        ,owner_id = ISNULL(@owner_id, owner_id)
        -- ...etc
    where
        object_id = @object_id

    return @@ROWCOUNT

END

So basically:

Update a column only if its corresponding parameter was provided, and leave the rest alone.

This works well enough, but as the ISNULL call will return the value of the column if the received parameter was null, will SQL Server optimize this somehow? This might be a performance bottleneck on the application where the table might be updated heavily (insertion will be uncommon so the performance there is not a problem). So I'm trying to figure out what's the best way to do this. Is there a way to condition the column expressions with something like CASE WHEN or something? The table will be indexed up the wazoo as well for read performance. Is this the best approach? My alternative at this point is to create the UPDATE expression in code (e.g. inline SQL) and execute it against the server. This would solve my doubts about performance, but I'd rather leave this in a stored proc if possible.

+1  A: 

ISNULL is the fastest way- the only way you'll improve is if you pass in NULL or the actual value, and do the ISNULL in the application.

Mike M.
I understand it's the fastest way in the context of the stored procedure, but will it be slow compared to generating the UPDATE statement in code? In other words, are all those ISNULL statements a performance problem to the extent that I'm better off just doing inline SQL?
kprobst
There should be no performance problem with this technique, but be careful and make sure that updating every column every time (even though you're setting it equal to itself) doesn't have any unintended side effects in triggers, Change Data Capture, etc.
Joe Stefanelli
+1  A: 

Take a look at Hugo Kornelis' blog post at http://sqlblog.com/blogs/hugo_kornelis/archive/2007/09/30/what-if-null-if-null-is-null-null-null-is-null.aspx. Scoll down a bit to the discussion on COALESCE vs. ISNULL. If portability is a future consideration, look at COALESCE.

However, from a performance perspective, take a look at Adam's performance-centric blog post at http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/performance-isnull-vs-coalesce.aspx. ISNULL is the speedier.

Your choice...

BTW, I have a bunch of SP's that are just like your example and have no performance issues using ISNULL. (Being a bit lazy, I like to type 6 vs. 8 chars and being a littel prone to finger-dyslexia, ISNULL is much easier to type :-) )

SAinCA
Thanks, I'll take a look at those.
kprobst