tsql - SQL Server query help to update the row instead of inserting -


I have two tables called T and T1. Problem: If a data is not available in # 1 T1 then I need to insert and if the data is avialable then I have to update. Let me say that I have got empty space before # T in the end date of # T1, then I need to get involved and if I get the data with the same ID with the same ID then I would end the # T1 Date must be updated in the date column and I do not want to have 2 ND entries in the table. How do I handle it? CREATE TABLE #t (ID INT, activedate VARCHAR (15), terminationdate VARCHAR (20)) CREATE TABLE # t1 (ID INT, VARCHAR (15 activedate), expiration date VARCHAR (20) ) Insert #t values ​​(1, '2006-01-01', NULL), (1, '2006-01-01', '2006-01-31') SELECT * FROM #t SELECT * # t1 (Different from selection #t *) on ds.id = al.id and ds.activedate = al.activedate and ds.terminationdate = al.terminationdate when not matched, then merge the merge into the # t1 DS (id , Activedate, terminationdate) values ​​(id, activedate, terminationdate) when matching and ds.terminationdate = '' then delete when matching and ds.id = al.id and ds.activedate = al.act Ivedate then set up the update ds.terminationdate = al .nation date;

Expect outside:

for the first time

  id activation end date 1 2006-01-01 null  < / Pre> 

If I got the terminationdate (value 2006- 8-30) forsame ID with the same active date then 2 record entry instead of record

  id activedate terminationdate 1 2006-01 Should be updated - 01 2006 - 8 - 30   

Note: If we can obtain the above mentioned question by changing it, it would be great otherwise it will be possible to share alternative methods. Received.

This scenario is exactly what was designed for. An example is:

  Merge dbo.Table1 on Source.Key = Table1.Key using dbo.Source update after matching Data1 = Source.Data1, Data2 = Source.Data2 when If the target does not match the target then (source.Key, Source.Data1, Source.Date2) do not match the source then delete the output $ action, isnull (Source.Key, Table1.Key), deleted.Data1, deleted Data2, inserted.Data1, inserted.Data2;    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -