22,209
社区成员
发帖
与我相关
我的任务
分享
declare @x int,@tsql varchar(6000)
select @x=cast(ceiling((rand()*7+11))as int) -- 产生随机数
select @tsql='select top '+rtrim(@x)+' name from table order by newid()'
exec(@tsql)
DECLARE @TOP VARCHAR(10)
SET @TOP=cast(ceiling((rand()*7+11))as INT)
EXEC('select top '+@TOP+' name from [table] order by newId()')
SQL2000貌似不支持TOP含表达式
可以考虑动态执行,如上