sql删除临时表查询的数据

注意:要在同一个sql方法里面写才可以

into #test 是把指定要删除的数据查询出来,然后创建临时表来存储,最后就可以直接删除掉临时表的数据,由于开头的临时表只能在本回话中使用,当回话结束的时候创建的临时表需要被删除掉才可以

select MsgID into #test from (

select MsgID,(ROW_NUMBER() over(order by MsgID asc))as rows_index

from Messages

) as db

where rows_index>30 and rows_index<=40

select * from #test --查询临时表的数据

delete from Messages where MsgID in(select MsgID from #test) --删除临时表的数据

drop table #test --删除临时表