我有个很简单的存储过程,要取符合条件的前n条记录, n作为参数传入,也就是@tops
ALTER PROCEDURE [dbo].[getNewsByCategory]
@categoryID smallint, @tops int
AS
select top @tops * from News where categoryID=@CategoryID 可总是提示错误,我把@tops换成实际的数字就可以,到底怎么回事?
ALTER PROCEDURE [dbo].[getNewsByCategory]
@categoryID smallint, @tops int
AS
select top @tops * from News where categoryID=@CategoryID 可总是提示错误,我把@tops换成实际的数字就可以,到底怎么回事?
@categoryID smallint, @tops int
AS
exec('select top '+ltrim(@tops)+' * from News where categoryID='+ltrim(@CategoryID ))
ALTER PROCEDURE [dbo].[getNewsByCategory]
@categoryID smallint, @tops int
AS
exec('select top '+rtrim(@tops)+' * from News where categoryID='+@CategoryID )
@categoryID smallint, @tops int
AS
declare @s varchar(8000)
set @s = 'select top '+ltrim(@tops)+' * from News where categoryID='+ltrim(@CategoryID )
exec(@s)
或者用动态sql
exec('select top '+ltrim( @tops) ' * from News where categoryID= '+ltrim( @CategoryID) )
@categoryID smallint, @tops int
AS
declare @sql varchar(8000);
set @sql='select top '+ltrim(@tops)+' * from News where categoryID='+ltrim(@CategoryID );
exec (@sql)
go
只能用动态SQL执行,
select top * from News where categoryID=@CategoryID