当前位置: 代码迷 >> Sql Server >> SQL语句出错,该怎么解决
  详细解决方案

SQL语句出错,该怎么解决

热度:5   发布时间:2016-04-24 09:58:03.0
SQL语句出错
declare @time3 int
declare @time4 int
declare @time5 varchar(100)
declare @table varchar(100)
set @time3='20140801'
set @time4='20140831'
--print @time3
while @time3<=@time4
begin
--print @time3
set @time5=cast(@time3 as varchar(100))
set @table='[dbo].['+@time5+'_TS_rmbcash_log]'
print @table
select account,SUM(point) from ''+@table+'' group by account
set @time3=@time3+1
end
在select account,SUM(point) from ''+@table+'' group by account这一句的 ''+@table+'' 这里提示语法错误,是怎么回事?
------解决思路----------------------
exec('select account,SUM(point) from '+@table+' group by account')

------解决思路----------------------
引用:
非常感谢,请问这个是什么问题啊?为什么加个exec就没错了


因为普通语句在执行时,表名称,表字段,都不能是动态,也就是不能是个变量。

只有当时动态执行语句时,也就是exec执行语句时,表名称才能是变量
------解决思路----------------------
表名称是变量的必须得放在exec中
  相关解决方案