Created
July 24, 2017 02:36
-
-
Save beginor/0b713e758f21bf616bcbd9cb879ab768 to your computer and use it in GitHub Desktop.
sqlserver merge table demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MERGE into dbo.water_quality_data_his AS target | |
USING dbo.water_quality_data_his_temp AS source | |
ON (target.id = source.id) | |
WHEN MATCHED | |
THEN UPDATE SET | |
target.[id] = source.[id], | |
target.[point_id] = source.[point_id], | |
target.[item_id] = source.[item_id], | |
target.[item_name] = source.[item_name], | |
target.[item_unit] = source.[item_unit], | |
target.[value] = source.[value], | |
target.[moni_time] = source.[moni_time], | |
target.[flag] = source.[flag], | |
target.[lower_limit] = source.[lower_limit], | |
target.[upper_limit] = source.[upper_limit], | |
target.[dec] = source.[dec], | |
target.[update_time] = source.[update_time] | |
when not matched by target | |
then insert | |
values( | |
source.[id], | |
source.[point_id], | |
source.[item_id], | |
source.[item_name], | |
source.[item_unit], | |
source.[value], | |
source.[moni_time], | |
source.[flag], | |
source.[lower_limit], | |
source.[upper_limit], | |
source.[dec], | |
source.[update_time] | |
); | |
--when not matched by source | |
-- then delete | |
--OUTPUT $action, Inserted.*, Deleted.*; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment