这样是不行的。可以按以下方法处理:
create #tempdb (nn int)insert #tempdb exec ('select count(*) from '+@tbl+' where cityid = 2')
select @num = nn from #tempdb
create #tempdb (nn int)insert #tempdb exec ('select count(*) from '+@tbl+' where cityid = 2')
select @num = nn from #tempdb
declare @paras varchar(100)
DECLARE @SQLString NVARCHAR(500)
set @paras ='1,3,5'
set @sqlstring=N'select @cnt=count(*) from sysobjects where id in ('+@paras+')'set @tcnt=0
execute sp_executesql
@sqlstring,
N'@cnt int output',
@cnt=@tcnt output
select @tcnt
select * from sysobjectsCreate Procedure GetMaxID
@TableName varchar(100), @ID int output
as
begin
declare @sql nvarchar(1000)
set @sql='select @ID = count(ID) from '+@TableName
exec sp_executesql @sql,N'@id int output',@id output
end
declare @num int
select @tbl = 'my_userinfo'
exec ('select count(*) as Count_I Into ##tmp from '+@tbl+' where cityid = 2')
select @num=Count_I From ##tmp
Drop table ##tmp
declare @tbl varchar(50)
declare @re int
select @tbl = 'my_userinfo'select @sql='select @num=count(*) from '+@tbl +' where cityid=2'
select @para='@num int output'
exec sp_executesql @sql,@para,@re output
select @re