Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Update All columns in table Expand / Collapse
Author
Message
Posted Monday, January 28, 2013 10:03 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 4, 2013 9:41 AM
Points: 20, Visits: 201
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
Post #1412749
Posted Tuesday, January 29, 2013 12:01 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:42 AM
Points: 3,440, Visits: 5,398
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1412773
Posted Tuesday, January 29, 2013 1:07 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, September 10, 2013 7:12 AM
Points: 273, Visits: 83
SQL 2008 R2 - Change Tracking may help you...
http://msdn.microsoft.com/en-us/library/cc280462(v=sql.105).aspx
Post #1412795
Posted Tuesday, January 29, 2013 3:39 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, December 11, 2014 9:19 PM
Points: 1,129, Visits: 1,164
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
Post #1412865
Posted Tuesday, January 29, 2013 5:10 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, December 2, 2014 9:20 AM
Points: 1,372, Visits: 1,567
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
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter

Post #1412942
Posted Tuesday, January 29, 2013 6:00 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, December 11, 2014 9:19 PM
Points: 1,129, Visits: 1,164
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
Post #1412974
Posted Tuesday, January 29, 2013 6:19 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 4:13 AM
Points: 954, Visits: 2,633
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
Post #1412985
Posted Tuesday, January 29, 2013 6:54 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, December 11, 2014 9:19 PM
Points: 1,129, Visits: 1,164
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
Post #1413020
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse