从Northwind数据库的Employees表中随机抽取5条数据
时间:2007-08-12 来源:wandrew
先建立一个表,再通过随机数函数取一个数,再将数据插入到所建立表中,最后查询出,你再去修改。
if object_id('tempdb..#abc')>0 drop table #abc
create table #abc(a int null,b varchar(20) null,c varchar(20) null)
declare @lastname varchar(20),@firstname varchar(20),@i int,@a varchar(20),@j int
set @j=0
while @j<=4
begin
set @i=cast(rand()*10 as int)
DECLARE a CURSOR FOR
select employeeid,lastname,firstname from employees where employeeid=@i
OPEN a
FETCH NEXT FROM a into @i,@lastname,@firstname
WHILE @@FETCH_STATUS = 0
BEGIN
insert into #abc select @i,@lastname,@firstname
FETCH NEXT FROM a into @i,@lastname,@firstname
END
CLOSE a
DEALLOCATE a
select @j=count(a) from #abc
end
select * from #abc