Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Merge in 2008 How ot move data Archive

Author  Topic 

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-05-17 : 03:34:43

Create Table #TableA (id int ,Fname nvarchar(23),Lname nvarchar(23))
insert into #TableA
Select 1,'Hi','India1' Union all
Select 2,'Think','India'

Drop Table #TableB
Create Table #TableB (id int ,Fname nvarchar(23),Lname nvarchar(23))
insert into #TableB
Select 3,'Hello','What'
Select 1,'Hi','India'

Create Table #Temp (id int ,Fname nvarchar(23),Lname nvarchar(23))
--This is Working Fine

Select * From #TAbleA
Select * from #TableB
Merge #TableA As TA
Using (Select * from #TableB) As TB
On TA.id=TB.id
When Matched Then
Update Set TA.Fname=TB.Fname
When not Matched Then
Insert (id,Fname,Lname) Values (TB.ID,TB.Fname,TB.Lname);
Select * From #TableA


--But This is not working

Select * From #TAbleA
Select * from #TableB
Merge #TableA As TA
Using (Select * from #TableB) As TB
On TA.id=TB.id
When Matched Then--No bigin and end not working
Update #Temp Set (id,Fname,Lname)=(Tb.id,Tb.Fname,Tb.Lname)
When not Matched Then
Insert (id,Fname,Lname) Values (TB.ID,TB.Fname,TB.Lname);
Select * From #TableA

In Love... With Me!

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2011-05-17 : 05:22:33
What is your question?
Go to Top of Page

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-05-17 : 07:18:15
How can I move data to archive before updating to main table tableA. See i have tableA with some data and my second table tableB have some repeted data those are already in tableA now before I update tableA comparing to TableB I need to move tableA data to archive

In Love... With Me!
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2011-05-17 : 10:03:50
Easiest way in this case would probably be a trigger on the table.
Go to Top of Page
   

- Advertisement -