sql问题,在线等待 !!!!!!!!!!!!!!!!!!

zhangyy79 2003-11-19 01:45:26
一个sql 由字符串拼成,sql内容:“select @row=count(*) from table ”
用sp_executesql 执行,我要得到@row的值即记录数目。
这个sql 应该怎么写
...全文
77 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-11-19
  • 打赏
  • 举报
回复
create table #临时表(结构和返回结构一样 int)

insert #临时表 exec('..........')

select * from #临时表
sunshareforever 2003-11-19
  • 打赏
  • 举报
回复
exec(@sql)
zhangyy79 2003-11-19
  • 打赏
  • 举报
回复
感谢各位,我补充一个问题,如果上述代码在存储过程中,想的到结果集(select * from table)应该怎么写
zjcxc 元老 2003-11-19
  • 打赏
  • 举报
回复
--如果你的字符串是组成的.就用:

declare @sql nvarchar(4000) --注意,一定要定义为unicode类型
set @sql='N'select @row=count(*) from table'

declare @row int
exec sp_executesql @sql,N'@row int out',@row out
zjcxc 元老 2003-11-19
  • 打赏
  • 举报
回复
declare @row int
exec sp_executesql N'select @row=count(*) from table',N'@row int out',@row out


--注意,第一个参数和第二个参数,必须是unicode类型的.所以字符串前加N
txlicenhe 2003-11-19
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2364/2364046.xml?temp=.829693
[交流]动态SQL语句
txlicenhe 2003-11-19
  • 打赏
  • 举报
回复
declare @sql nvarchar(4000),@row int
set @sql = 'Select @row=count(*) from table'
exec sp_executesql @sql,N'@row int output',@row output
select @row

pengdali 2003-11-19
  • 打赏
  • 举报
回复
declare @sql nvarchar(1000),@id int
set @sql='select @row=count(*) from [table]'

exec sp_executesql @sql,N'@row int output',@id output


select @id 结果

34,875

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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