关于解决SQL语句select top 后面不能接变量的问题(sql 2000)

关于解决SQL语句select top 后面不能接变量的问题(sql 2000),采用动态的Sql语句来处理
 
ALTER proc [dbo].[sp_GetSubjectByTypeAndNum]
@type VarChar(50),
@num  Int 
as
declare @a int
set @a=@num 
declare @sql nvarchar(4000)
 
 
if @type='Single'
begin
--根据参数设置查询单选题Sql语句
--select top (@num) ID,Title,AnswerA,AnswerB,AnswerC,AnswerD from SingleProblem order by newid()
set @sql = 'select top '+ cast(@a as nvarchar(10)) + ' * from SingleProblem order by newid()' execute(@sql)
end
posted @ 2012-06-28 22:35  AllenLYU  阅读(1173)  评论(0编辑  收藏  举报
levels of contents