当前位置: 代码迷 >> Sql Server >> 关于exec(.) 执行时,总是报错呢?帮忙看下哦解决方案
  详细解决方案

关于exec(.) 执行时,总是报错呢?帮忙看下哦解决方案

热度:95   发布时间:2016-04-27 20:45:07.0
关于exec(...) 执行时,总是报错呢?帮忙看下哦
declare   @count   int
declare   @table   varchar(50)
set   @table   =   'webgame_sys_user '
exec( 'SELECT   '[email protected]+ '   =   COUNT(user_id)   FROM   '+   @table)
print   @count

提示=号附近有语法错误   ?

------解决方案--------------------
declare @count int
declare @table varchar(50)
declare @sql nvarchar(2000)
set @table = 'webgame_sys_user '
set @sql = 'SELECT @count = COUNT(user_id) FROM '+ @table
EXEC sp_executesql @sql,N '@count int output ',@count output
print @count
------解决方案--------------------
declare @count int, @sql Nvarchar(1000)
declare @table varchar(50)
set @table = 'webgame_sys_user '
set @sql = ' SELECT @count = COUNT(user_id) FROM '+ @table
exec sp_executesql @sql, N '@count int output ', @count output
print @count
------解决方案--------------------
declare @count int, @sql nvarchar(4000)

declare @table varchar(50)

set @table = 'webgame_sys_user '

set @sql= 'select @a=count(user_id) from ' + @table
exec sp_executesql @sql,N '@a int output ',@count output
select @count


------解决方案--------------------
用sp_executesql 得到返回参数
  相关解决方案