存储过程问题 如何让 exec执行的SQL语句中的数据赋值给存储过程中的 OUTPUT变量?

jim.ma 2005-08-08 02:02:13
create proc sp_test
@KW nvarchar(50),
@PT int=100,
@PC int=0 output
as
declare @sql as nvarchar(2000)
set @sql='select top '+cast(@PT as nvarchar(10))+' @PC=count(1),t.id,t.title,t.pic from tupian t inner join ContainsTable(tupian,*,'''+ @KW +''') t2 on t.[id]=t2.[key] where t.isshow=1 order by t2.rank desc'
exec(@sql)
return
go

declare @f int
execute sp_test 10,@f output
print @f
go
------------------------------------------------------------------------
错误信息:
服务器: 消息 137,级别 15,状态 1,行 1
必须声明变量 '@PC'。
-------------------------------------------------------------------------
我想select top 的同时用 count统计结果集 并将结果结数通过output 返回。
直接用 select top @PT @PC=count(1),t.id,t.title,t.pic from tupian t inner join ContainsTable(tupian,*,@KW) as t2 on t.[id]=t2.[key] where t.isshow=1 order by t2.rank desc 不行
用了全文索引
用上面的写法执行的SQL 有不能把存储过程中的@PC 赋值。 这我该怎么实现?

...全文
504 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
vivianfdlpw 2005-08-08
  • 打赏
  • 举报
回复
exec(@sql)
=============>
exec sp_executesql @sql,N'@pc int output',@pc output
rivery 2005-08-08
  • 打赏
  • 举报
回复
sql 确实不能实现下面的效果。
declare @a int
select @a=a,b,c from tablename
即:即赋值,又检索。
jim.ma 2005-08-08
  • 打赏
  • 举报
回复
服务器: 消息 141,级别 15,状态 1,行 1
向变量赋值的 SELECT 语句不能与数据检索操作结合使用。

看来是 SQL 不能这样:(
jim.ma 2005-08-08
  • 打赏
  • 举报
回复
更正执行SP的代码
declare @f int
execute sp_test '图片',1000,@f output
print @f
go

就是有没有办法实现 select top @PT @PC=count(1),* from ......查询条件。在存储过程中一句里实现。

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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