Update All columns in table

  • HI ,

    I have table EMP(source), in this i have 5 columns

    we don't know which column was updated in the source

    but i want load to destination table with updated data only

    how to write update statement for this requirement

    plz help me

    Regards

    Arjun

  • Two ways I know of to do this:

    1. In an UPDATE trigger, you can query UPDATE() function (http://msdn.microsoft.com/en-us/library/ms187326.aspx) to tell you if the column was SET by the UPDATE statement. Unfortunately, it does not tell you whether it changed or not (but you could test this by comparing the values for INSERTED.column vs. DELETED.column using those pseudo tables.

    2. Use an OUTPUT clause of UPDATE to dump the updated results to a temporary table. I believe you can also reference INSERTED.* and DELETED.* psuedo tables there to dump all columns into the temp table. Then you have to query back from the temp table to see what was changed.


    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

  • SQL 2008 R2 - Change Tracking may help you...

    http://msdn.microsoft.com/en-us/library/cc280462(v=sql.105).aspx

  • Try this one , It might help ;

    Merge into desttable as target

    using

    (

    select col1, col2,col3,col4,col5 from sourcetable

    except

    select col1, col2,col3,col4,col5 from desttable

    ) as source

    on target.col1=source.col1 -- (use primary key )

    when matched then

    update set target.col1=source.col1,

    .......

    when not matched bye target then insert

    (col1,col2,col3,col4,col5) values (source.col1,.....)

    merge statement to update or insert or delete the records.

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • Using merge (as pointed out by demonfox) you can easily user INSERTED.* and DELETED.* (as mentionded by Dwain) after MERGE in order to capture, columns and values which are being updated/inserted/deleted.

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • Lokesh Vij (1/29/2013)


    Using merge (as pointed out by demonfox) you can easily user INSERTED.* and DELETED.* (as mentionded by Dwain) after MERGE in order to capture, columns and values which are being updated/inserted/deleted.

    Yes and Inserted.col1 , Inserted.col2 woudl work for new data updated or inserted ;

    if you are handling delete also in the Merge statement.

    then it gets a little difficult to handle insert , delete and update data .

    As Insert and Update can be captured by Inserted.* ; but the deleted require Deleted.* , and in the merge statement there is only one Output clause allowed.

    In that case you may have to consider using cases by identifying $action property.

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

  • Demon correct, but you can use the $Action on the output statement will show the action that was performed for that row of data.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • Jason-299789 (1/29/2013)


    Demon correct, but you can use the $Action on the output statement will show the action that was performed for that row of data.

    Exactly Jason ; That ought to be the requirement. This is the formatted code with delete and case statement, not tested , but should work fine..

    CREATE TABLE #author2 (cola VARCHAR(50),colb VARCHAR(50), action_flag VARCHAR(10) )

    CREATE TABLE dbo.author1

    ( cola VARCHAR(50), colb VARCHAR(50) )

    CREATE TABLE dbo.authors

    ( au_fname VARCHAR(100), au_lname VARCHAR(100)

    )

    MERGE INTO authors AS target

    using (SELECT cola,

    colb

    FROM author1

    EXCEPT

    SELECT au_fname,

    au_lname

    FROM authors) AS source

    ON target.au_fname = source.cola

    WHEN matched THEN

    UPDATE SET target.au_lname = source.colb

    WHEN NOT matched BY target THEN

    INSERT (au_fname,

    au_lname)

    VALUES(source.cola,

    source.colb)

    WHEN NOT matched BY source THEN

    DELETE

    output ( CASE

    WHEN $action = 'DELETE' THEN deleted.au_fname

    ELSE inserted.au_fname

    END )au_fname,

    ( CASE

    WHEN $action = 'DELETE' THEN deleted.au_lname

    ELSE inserted.au_lname

    END )colb,

    $action

    INTO #author2;

    ~ demonfox
    ___________________________________________________________________
    Wondering what I would do next , when I am done with this one :ermm:

Viewing 8 posts - 1 through 7 (of 7 total)

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