Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update All columns in table


Update All columns in table

Author
Message
Arjun123
Arjun123
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
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
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4255 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Suresh-209155
Suresh-209155
SSC Veteran
SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)

Group: General Forum Members
Points: 273 Visits: 83
SQL 2008 R2 - Change Tracking may help you...
http://msdn.microsoft.com/en-us/library/cc280462(v=sql.105).aspx
demonfox
demonfox
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1219 Visits: 1192
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
Lokesh Vij
Lokesh Vij
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1564 Visits: 1599
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


demonfox
demonfox
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1219 Visits: 1192
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
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1134 Visits: 3229
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
demonfox
demonfox
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1219 Visits: 1192
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search