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
 Old Forums
 CLOSED - General SQL Server
 How to Update an existin table using a Temperary table cont

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-12-15 : 07:57:15
Soorej writes "I have a subscriber table with fields of my interest as ID,subscriber_id,status,rateplan, plan_start_date,plan_end_date,term_date

ID is the pk,, there can be many entries with the same subscriber_id as and when he chnges the rateplan . But there will be a record with plan_end_date as an infinite date. Also the term_date will be infinite in this case..

Then I have a temp_subscriber table with the same structure. In this I have the updates for the subscriber table.

I have to compare the data in this temp_subscriber table with the subscriber table..

If the same record exists in the subscriber table . No alteration to subscriber table

If there is record with the same subscriber_ID but a change in status or rateplan
then update the plan_end_date of subscriber table with plan_start_date in temp_subscriber table
also insert this record in temp_subscriber table to subscriber table
( Update + Insert)

If the record in temp_subscriber table is not in subscriber table
just insert the record ..

( Records are compared using subscriber_id field )

This is to be done in MS SQL Server 2005"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-15 : 10:08:30
General approach

Update t1
set col=t2.col,..
from table1 t1 inner join table2 t2
on t1.keycol=t2.keycol

Insert into table1(cols)
Select cols from table2 t2
where not exists(select * from table1 where keycol=t2.keycol)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -