急需一个关于存储过程的问题

oralxc 2003-08-24 04:59:54
写存储过程的时候在TOP时TOP参数也用变量怎么写
@id int,
@top int

select top @top * from DTable where id=@id

这个肯定是错的,大家请帮帮小弟啊~先谢啦~
...全文
65 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
oralxc 2003-08-28
  • 打赏
  • 举报
回复
谢谢大家了~
liuyun2003 2003-08-25
  • 打赏
  • 举报
回复
exec('select top '+cast(@top as varchar)+' * from DTable where id='+cast(@id as varchar))
goneaway 2003-08-25
  • 打赏
  • 举报
回复
declare @id int
declare @top int
declare @sql_str nvarchar(4000)
set sql_str='select top '+cast(@top as nvarchar)+' * from DTable where id='+cast(@id as nvarchar)
EXECUTE sp_executesql sql_str
goneaway 2003-08-25
  • 打赏
  • 举报
回复
@id int,
@top int
@sql_str nvarchar(4000)
set sql_str='select top '+cast(@top as nvarchar)+' * from DTable where id='+cast(@id as nvarchar)
EXECUTE sp_executesql sql_str
yangvxin1 2003-08-24
  • 打赏
  • 举报
回复
搞错了。id为int要转化.呵呵.不好意思.

declare @p1 varchar(100)

set @p1='select top '+cast(@top as varchar)+' * from DTable where id='+cast(@id as varchar)

exec(@p1)
yangvxin1 2003-08-24
  • 打赏
  • 举报
回复
搞错了。id为int要转化.

declare @p1 varchar(100)

set @p1='select top '+cast(@top as varchar)+' * from DTable where id='+cast(@id as varchar)

exec(@p1)
zarge 2003-08-24
  • 打赏
  • 举报
回复
declare @cmd nvarchar(255)

Set @cmd = 'select top ' + cast(@top as nvarchar)
+ ' * from DTable where id=' + cast(@id as nvarchar)

exec sp_executesql @cmd
yangvxin1 2003-08-24
  • 打赏
  • 举报
回复
好象是:
declare @p1 varchar(100)

set @p1='select top '+cast(@top as varchar)+' * from DTable where id='+@id

exec(@p1)

22,207

社区成员

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

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