1、sql server2005以上可以直接加()就行。
DECLARE @number INT SELECT @number=2 SELECT TOP (@number) * FROM dbo.[table] strSql.Append(" SELECT top (@top) "); parameter.Add(DbParameters.CreateDbParameter("@top", 100));
2、拼凑字符串
DECLARE @number INT,@sql VARCHAR(500) SELECT @number=2 SET @sql='select top '+STR(@number)+' * from [User]' EXEC (@sql)
3、利用rownumber
1 DECLARE @number INT 2 SELECT @number=2 3 ; 4 WITH t AS( 5 SELECT *,ROW_NUMBER() OVER(ORDER BY Id) AS rowNumber FROM [User]) 6 SELECT UserName,UserPwd FROM t WHERE t.rowNumber<=@number
注意:with as语句在批处理中,需要加;
标签:top,number,参数,select,sql,DECLARE,SELECT From: https://www.cnblogs.com/qingjiawen/p/17236542.html