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.
| Author |
Topic |
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-04-15 : 13:51:52
|
I've table and rows as follow,declare @t1 table(idx int identity(1,1), TID varchar(20), SeatN varchar(5), Posi varchar(20), selldte datetime);insert into @t1 values ('1989','5A','100','20100318 6:40PM');insert into @t1 values ('1989','5A','110','20100318 8:40PM');insert into @t1 values ('2890','1B','100','20100317 7:40PM');insert into @t1 values ('2341','1C','100','20100313 2:40PM');At this level, my record in @t1 in order by selldte as follow,idx | TID | SeatN | Posi | SellDte----------------------------------------------------------4 2341 1C 100 2010-03-13 14:40:00.0003 2890 1B 100 2010-03-17 19:40:00.0001 1989 5A 100 2010-03-18 18:40:00.0002 1989 5A 110 2010-03-18 20:40:00.000 Now, me create a temp table as follow,create table #tPosi(idx int identity(1,1) primary key clustered, TID varchar(20), SeatN varchar(5), Posi varchar(20));alter table #tPosi add constraint tPosi01 unique(TID,SeatN); My question is1. I want to insert my record in @t1 into #tPosi2. If record found in #tPosi then update usingupdate #tPosi set Posi=Posi where TID=TID and SeatN=SeatN 3. If record not found in #tPosi then insert usinginsert into #tPosi values(TID,SeatN,Posi); Really need help. |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2010-04-15 : 15:16:17
|
| you can run 2 separate statements - an update by joining the two tables and another insert with a not exists condition.Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-15 : 16:46:33
|
USE MERGE statement:I also assumed you want the latest date in #tPosi if there is a duplicated TID and SeatN.MERGE #tPosi AS targetUSING (SELECT TID, SeatN, Posi from (SELECT TID, SeatN, Posi, row_number() over(partition by TID, SeatN order by SellDte desc) as rnk from @t1)t where rnk = 1) as source (TID, SeatN, Posi)ON (target.TID = source.TID and target.SeatN = source.SeatN)WHEN MATCHED THENUPDATE SET Posi = source.PosiWHEN NOT MATCHED THENinsert (TID, SeatN, Posi) values(TID,SeatN,Posi); quote: Originally posted by Delinda I've table and rows as follow,declare @t1 table(idx int identity(1,1), TID varchar(20), SeatN varchar(5), Posi varchar(20), selldte datetime);insert into @t1 values ('1989','5A','100','20100318 6:40PM');insert into @t1 values ('1989','5A','110','20100318 8:40PM');insert into @t1 values ('2890','1B','100','20100317 7:40PM');insert into @t1 values ('2341','1C','100','20100313 2:40PM');At this level, my record in @t1 in order by selldte as follow,idx | TID | SeatN | Posi | SellDte----------------------------------------------------------4 2341 1C 100 2010-03-13 14:40:00.0003 2890 1B 100 2010-03-17 19:40:00.0001 1989 5A 100 2010-03-18 18:40:00.0002 1989 5A 110 2010-03-18 20:40:00.000 Now, me create a temp table as follow,create table #tPosi(idx int identity(1,1) primary key clustered, TID varchar(20), SeatN varchar(5), Posi varchar(20));alter table #tPosi add constraint tPosi01 unique(TID,SeatN); My question is1. I want to insert my record in @t1 into #tPosi2. If record found in #tPosi then update usingupdate #tPosi set Posi=Posi where TID=TID and SeatN=SeatN 3. If record not found in #tPosi then insert usinginsert into #tPosi values(TID,SeatN,Posi); Really need help.
|
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-04-15 : 23:22:07
|
quote: Originally posted by hanbingl USE MERGE statement:I also assumed you want the latest date in #tPosi if there is a duplicated TID and SeatN.MERGE #tPosi AS targetUSING (SELECT TID, SeatN, Posi from (SELECT TID, SeatN, Posi, row_number() over(partition by TID, SeatN order by SellDte desc) as rnk from @t1)t where rnk = 1) as source (TID, SeatN, Posi)ON (target.TID = source.TID and target.SeatN = source.SeatN)WHEN MATCHED THENUPDATE SET Posi = source.PosiWHEN NOT MATCHED THENinsert (TID, SeatN, Posi) values(TID,SeatN,Posi); quote: Originally posted by Delinda I've table and rows as follow,declare @t1 table(idx int identity(1,1), TID varchar(20), SeatN varchar(5), Posi varchar(20), selldte datetime);insert into @t1 values ('1989','5A','100','20100318 6:40PM');insert into @t1 values ('1989','5A','110','20100318 8:40PM');insert into @t1 values ('2890','1B','100','20100317 7:40PM');insert into @t1 values ('2341','1C','100','20100313 2:40PM');At this level, my record in @t1 in order by selldte as follow,idx | TID | SeatN | Posi | SellDte----------------------------------------------------------4 2341 1C 100 2010-03-13 14:40:00.0003 2890 1B 100 2010-03-17 19:40:00.0001 1989 5A 100 2010-03-18 18:40:00.0002 1989 5A 110 2010-03-18 20:40:00.000 Now, me create a temp table as follow,create table #tPosi(idx int identity(1,1) primary key clustered, TID varchar(20), SeatN varchar(5), Posi varchar(20));alter table #tPosi add constraint tPosi01 unique(TID,SeatN); My question is1. I want to insert my record in @t1 into #tPosi2. If record found in #tPosi then update usingupdate #tPosi set Posi=Posi where TID=TID and SeatN=SeatN 3. If record not found in #tPosi then insert usinginsert into #tPosi values(TID,SeatN,Posi); Really need help.
Me using SQL Server 2005, i'm afraid there's no MERGE function |
 |
|
|
ColdCoffee
Starting Member
1 Post |
Posted - 2010-04-16 : 07:08:00
|
Hey Delinda..Here is the code for you.. this will update if an match is found and will insert if no match is found between the tables..Tell us if this worked for u--== FIRST LETS UPDATE THE DATA INTO THE SECOND TABLE--== IF THE ROWS DOES EXISTUPDATE T2SET T2.Posi = T1.PosiFROM #tPosi T2INNER JOIN #T1 T1ON ( T1.SeatN = T2.SeatN AND T1.TID = T2.TID )WHERE T1.selldte = (SELECT MAX(selldte) FROM #T1 WHERE TID = T1.TID AND SeatN = T1.SeatN )--== THEN LETS INSERT THE DATA INTO THE SECOND TABLE--== IF THE ROWS DOES NOT EXISTINSERT INTO #tPosi (TID, SeatN,Posi)SELECT T1.TID, T1.SeatN,T1.PosiFROM #T1 T1LEFT JOIN #tPosi T2ON ( T1.TID = T2.TID AND T1.SeatN = T2.SeatN )WHERE ( T2.SeatN IS NULL AND T2.TID IS NULL )ORDER BY T1.selldte |
 |
|
|
|
|
|
|
|