Created
April 30, 2017 10:01
-
-
Save reshmee011/9dcb7d7bd939a26de6b18b6fa378e72b to your computer and use it in GitHub Desktop.
This file contains 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
BEGIN TRAN | |
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tmp_table_value') and xtype='U') | |
drop table #tmp_table_value | |
CREATE TABLE #tmp_table_value | |
([table_id] [int] NOT NULL, | |
[value_date] [datetime] NOT NULL, | |
[raw_value] [decimal](12, 6) NULL | |
) | |
BULK INSERT #tmp_table_value FROM 'C:\data.csv' WITH ( FIRSTROW = 2 , FIELDTERMINATOR = ',' , rowterminator = '\n',KEEPNULLS) | |
--table_value is the target table | |
-- the temp table tmp_table_value is the source table | |
MERGE table_value AS T | |
USING #tmp_table_value AS S | |
ON (T.table_id = S. table_id and T.value_date = S.value_date) | |
WHEN NOT MATCHED | |
THEN INSERT(table_id, value_date, raw_value ) | |
VALUES | |
( S.table_id, S.value_date, S.raw_value) | |
WHEN MATCHED AND (T.raw_value != S.raw_value ) | |
THEN UPDATE SET T.raw_value = S.raw_value ; | |
select * from table_value order by value_date desc | |
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tmp_table_value') and xtype='U') | |
drop table #tmp_table_value | |
ROLLBACK TRAN; | |
--COMMIT TRAN; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment