日期:2014-05-18  浏览次数:20597 次

Help: 请问如下语句有什么错误?
/****** Script for SelectTopNRows command from SSMS ******/
declare @i int
set @i=400001

while @i < 400003
begin
declare @tbname varchar(12)
declare @sql varchar(255)
set @tbname = 'Min50' + STR(@i)
set @sql = 'select * from ' + @tbname + ' where Volumn != 0'
exec(@sql)
set @i = @i + 1
end

一直报错误:
Msg 170, Level 15, State 1, Line 1
第 1 行: '400' 附近有语法错误。
Msg 170, Level 15, State 1, Line 1
第 1 行: '400' 附近有语法错误。

求高人指点。。。

------解决方案--------------------
这样试试
SQL code
declare @i int
set @i=400001

while @i < 400003
begin
declare @tbname varchar(50)
declare @sql varchar(255)
set @tbname = 'Min50' + RTRIM(@i)
set @sql = 'select * from ' + @tbname + ' where Volumn != 0'
exec(@sql)
set @i = @i + 1
end

------解决方案--------------------
SQL code
/****** Script for SelectTopNRows command from SSMS ******/
declare @i int
set @i=400001

while @i < 400003
begin
declare @tbname varchar(12)
declare @sql varchar(255)
set @tbname = 'Min50' + convert(varchar(6),@i)
set @sql = 'select * from ' + @tbname + ' where Volumn != 0'
exec(@sql)
set @i = @i + 1
end