如果B中的記錄是唯一的,那麼你可以使用這樣的查詢
-------------- 編輯 --------- ----------
如果B可有多個記錄,您可以使用ROW_NUMBER()funcion與分區
insert into TestA
select b.*,
(select max([rank]) from TestA where col1 = b.col1 and col2 = b.col2 and col3 = b.col3)
+ row_number()over (partition by col1, col2, col3 order by col1, col2,col3 asc) as N
from TestB b
------------- - END編輯 -------------------
注:我改名錶:種皮和TESTB
insert into TestA
select b.*, (select max([rank])+1 from TestAwhere col1 = b.col1 and col2 = b.col2 and col3 = b.col3)
from TestB b
或用JOIN這樣
insert into testa
select b.*, mr+1 from TestB b
join
(select col1, col2, col3, max([rank]) as mr
from TestA A
group by col1, col2, col3) as M
on
b.col1 = M.col1 and b.col2 = M.col2 and b.col3 =M.col3
嗨,感謝您的幫助。第一個代碼不能工作,因爲運行列不在表b中。我嘗試了第二個。這樣做的問題是,如果表B的重複行與第一行相同,那麼它對第二個條目也具有相同的等級。它必須增加。 –
我修改了查詢以與表B中的多個條目一起工作 –
Genius man ... :)很好...修改後的代碼按預期工作.. –