merge data dynamically?

  • Hi all, I'm curious if something like this is possible in SQL Server 2008:

    I have table of standardized data that I'm merging into from an Import table.

    This import table may have the [ContactName]. I one row in the import table has the contact name then every row would have the contact name.

    What I'd like to be able to do is something like this in the UPDATE portion of the Merge (I don't care about the insert phase, since I can insert a null [ContactName] since I'm not overwritting an existing [ContactName]

    MERGE INTO [dbo].[Standardized] AS [target]

    USING [dbo].[ImportDestination]

    AS [Source]

    ON [Source].[Key] = [Target].[Key]

    WHEN MATCHED

    THEN UPDATE

    SET[ContactName] = CASE WHEN source.[ContactName] IS NOT NULL THEN source.[ContactName] else target.[ContactName];

    Is anything like that possible and if the code above works it would only work on a row by row bases. Is it possible to figure out if any [ContactName] has data and if so possibly overwrite an old [ContactName] with a null?

  • jeff.born (9/2/2014)


    Hi all, I'm curious if something like this is possible in SQL Server 2008:

    I have table of standardized data that I'm merging into from an Import table.

    This import table may have the [ContactName]. I one row in the import table has the contact name then every row would have the contact name.

    What I'd like to be able to do is something like this in the UPDATE portion of the Merge (I don't care about the insert phase, since I can insert a null [ContactName] since I'm not overwritting an existing [ContactName]

    MERGE INTO [dbo].[Standardized] AS [target]

    USING [dbo].[ImportDestination]

    AS [Source]

    ON [Source].[Key] = [Target].[Key]

    WHEN MATCHED

    THEN UPDATE

    SET[ContactName] = CASE WHEN source.[ContactName] IS NOT NULL THEN source.[ContactName] else target.[ContactName];

    Is anything like that possible and if the code above works it would only work on a row by row bases. Is it possible to figure out if any [ContactName] has data and if so possibly overwrite an old [ContactName] with a null?

    Possible. But since MERGE has some overhead, why don't you just do this?

    UPDATE [target]

    SET [ContactName] = CASE WHEN source.[ContactName] IS NOT NULL THEN source.[ContactName] else target.[ContactName]

    FROM [dbo].[Standardized] [target]

    JOIN[dbo].[ImportDestination] [Source]

    ON [Source].[Key] = [Target].[Key];


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

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

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