T-sql范围查询

时间:2010-08-13 20:19:19

标签: sql-server tsql

我想做类似下面的事情。 我有下面的伪逻辑。 只是想知道如何实现这个:

delete B
from
TableA A with (nolock), TableB B with (nolock) 
where A.BeginNumber <= B.startipnum and A.Endnumber >= B.endipnum

delete A
from
TableA A with (nolock), TableB B with (nolock) 
where A.BeginNumber => B.startipnum and A.Endnumber <= B.endipnum

update B
set endipnum = startnumber - 1
from
TableA A with (nolock), TableB B with (nolock) 
where A.BeginNumber < B.startipnum and A.Endnumber <= B.endipnum

update B
set startipnum = endnumber + 1
from
TableA A with (nolock), TableB B with (nolock) 
where A.BeginNumber >= B.startipnum and A.Endnumber > B.endipnum

样本数据

表A

BeginNumber EndNumber
----------      ---------
16843009    16843009
16843009    16843010
16843009    16843013
16843009    16843016
33686020    33686024

表B

startipnum  endIPNum
----------      ---------
1488047401  1488047401
1504824617  1873923369
1890700585  1907477801
1924255017  2142358825
2159136041  2276576553

两个表之间没有要连接的列。 请帮忙。

谢谢!

1 个答案:

答案 0 :(得分:0)

没有什么可以阻止你使用例如on子句执行连接ON 1=1 - 将全部加入所有人 - 同样,CROSS JOIN实现相同(并且是做TBH的更好方法)。