日期:2014-05-17  浏览次数:20415 次

请教SQL问题
怎么把下面语句的执行结果,插入到一个新表,再从原表把此记录删除
declare @n int
set @n= (select top 1 cno from tb321 where cno < 10 order by newid())
declare @strSql nvarchar(1000)
set @strSql='select  top '+cast(@n as varchar) + 'cno,date,goods,cname,color long,size,quantity,price,year,range,season,category,brand from tb321 order by newid()'
exec(@strSql)
)

------解决方案--------------------
引用:
其实我就要随机抽取几条不重复记录(在10条以内),然后插入到新表,再从原表删除,每次出入记录加个流水号字段表示出出入记录就行


你再试试这样行吗;
--先建立一个表tb321_delete

select identity(int,1,1) as id,*
       into tb321_delete
from tb321
where 1 <> 1

declare @n int
set @n= (select top 1 cno from tb321 where cno < 10 order by newid())

--没有用动态语句,直接用的查询
;with t
as
(
select top (@n) * 
from tb321
order by newid()  --随机获取数据
)

--删除随机数据,同时把结果保存到 tb321_delete
delete t  
output deleted.* into tb321_delete