Select Top 后面能否跟参数???帮我解决

breakerfish 2003-09-12 04:05:55
Select Top 后面能否跟参数
我下面的存储过程是错误的,那位帮我解决一下
Create Procedure GetSpecial
---@top_sp Int
As
Declare @top_sp int
Set @top_sp = 49
Select top '+@top_sp+' * From Special --就是这里说语法不对呀

GO
...全文
408 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wally_wu 2003-09-12
  • 打赏
  • 举报
回复
Create Procedure GetSpecial
@top_sp Int
As
declare @sql varchar(8000)
set @sql='Select top '+CONVERT(varchar, @top_sp)+' * From Special'
exec(@sql)

GO
happy_0325 2003-09-12
  • 打赏
  • 举报
回复
declare @sql nvarchar(1000)
declare @top_sp int
set @top_sp=5
select @sql='select top '+convert(nvarchar(10),@top_sp) +' * from special'
exec sp_executesql @sql
我试过了,没问题
dafu71 2003-09-12
  • 打赏
  • 举报
回复
Create Procedure GetSpecial
---@top_sp Int
As
Declare @top_sp int
Set @top_sp = 49
exec( 'Select top '+ cast(@top_sp as varchar(10)) +' * From Special') --就是这里说语法不对呀

GO
aierong 2003-09-12
  • 打赏
  • 举报
回复


declare @sql nvarchar(1000)
select @sql='select top '+convert(nvarchar(10),@top_sp) +' * from special'
exec sp_executesql @sql
lfengxu 2003-09-12
  • 打赏
  • 举报
回复
Create Procedure GetSpecial
@top_sp Int
As
declare @sql varchar(8000)
set @sql='Select top '+convert(varchar(50),@top_sp)+' * From Special'
exec(@sql)
zjcxc 2003-09-12
  • 打赏
  • 举报
回复
改为:
Create Procedure GetSpecial
@top_sp Int
As
declare @sql varchar(8000)
set @sql='Select top '+cast(@top_sp as varchar)+' * From Special'
exec(@sql)

GO
liuyun2003 2003-09-12
  • 打赏
  • 举报
回复
Select top '+@top_sp+' * From Special 改为
exec('select top '+cast(@top_sp as varchar(10)) +' * from special')
arrow_gx 2003-09-12
  • 打赏
  • 举报
回复
改一下就可以了
Create Procedure GetSpecial
---@top_sp Int
As
Declare @SQL varchar(200)
Declare @top_sp varchar(200)
Set @top_sp = '49'
set @sql='Select top '
set @sql=@sql+@top_sp+' * From Special'

exec @sql
GO

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧