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
 moving data from one table to another

Author  Topic 

Naveensrcl
Starting Member

8 Posts

Posted - 2011-10-06 : 03:49:31
hi experts,

i need to move records from one table another.



create table stagingtable
(
id int not null primary key,
name varchar(10),
city varchar(100),
address varchar(10),
)

create table paxtable
(
id int not null primary key,
name varchar(10),
city varchar(10),
address varchar(10),
)


create table logtable
(
id int not null identity primary key,
rownum int,
stepmessage varchar(100),
errormessage varchar(100),
transactiondate datetime
)

---- sample data for staging table
insert into stagingtable
select 1,'name1','city1','address1' union all
select 2,'name2','abcdefghijklmnop','address2' union all
select 3,'name3','city3','address3' union all

---- sample data for pax table
insert into paxtable
select 1,'name','city','address'


I need to prepare a stored procedure . the logic is follows

I need to move records from stagingtable to paxtable.
while moving records ,
If id already exists in paxtable then i need to update paxtable with stagingtable data
Else i need to insert into paxtable

insertng rows/updated rows are should be delete from stagingtable.

in logtable i need to store details for each row of staging table
logtable should have data like below
id rownum stepmessage errormessage
1 1 row1:updated null
2 2 row2:insertionfailes error
3 3 row3:insertionsucesses null


Please help me..


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-06 : 05:02:11
[code]
declare @processed table
(
id int
)


UPDATE p
SET p.name = s.name,
p.city = s.city,
p.address = s.address
FROM paxtable p
output inserted.id into @processed
INNER JOIN stagingtable st
on st.id = p.id

insert into paxtable
output inserted.id into @processed
select id,name,city,address
from stagingtable

delete s
from stagingtable s
join @processed p
on p.id = s.id

for loging add a insert/update/delete trigger on staging table and put a logic to populate logtable based on inserted and deleted tables
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-10-06 : 05:17:28
but the insert statement will override the updated rows...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-10-06 : 05:18:34
means the insert needs a WHERE clause...left join


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Naveensrcl
Starting Member

8 Posts

Posted - 2011-10-06 : 05:32:00
In stored procedure i need to process row by row

In case of second row insertion will fail due to varchar size.
Go to Top of Page
   

- Advertisement -