将varchar值'*'转换为数据类型int时转换失败

时间:2011-10-05 02:04:37

标签: sql-server-2005 while-loop

当我在sql server 2005中运行嵌套while循环时,我遇到了这个问题。

我的外部循环得到一次迭代,然后我的内部循环得到它的完整的第一次迭代,但是我的内部循环之后的语句永远不会被执行,这似乎打破了一切。

我现在迷路了,我觉得我很容易失去一些东西,非常感谢任何帮助。

while exists(select top 1 ident from #tmpAttorneyImport (nolock) where parsed = 0 and zipcode <> '')
begin

set @intCurrentIdent = 0
set @vcrCurrentAttonreyName = ''
set @vcrCurrentZip = ''

select top 1 @intCurrentIdent = ident from #tmpAttorneyImport (nolock) where parsed = 0
select @vcrCurrentAttonreyName = ltrim(rtrim(attorneyname)) from #tmpAttorneyImport (nolock) where ident = @intCurrentIdent
select @vcrCurrentZip = ltrim(rtrim(zipcode)) from #tmpAttorneyImport (nolock) where ident = @intCurrentIdent

if(len(@vcrCurrentZip) > 3)
 begin

    set @vcrMinZip = ''
    set @vcrMaxZip = ''

    select @vcrMinZip = ltrim(rtrim(left(@vcrCurrentZip, 3)))
    select @vcrMaxZip = ltrim(rtrim(right(@vcrCurrentZip, 3)))

    while(convert(int, @vcrMinZip) <= convert(int, @vcrMaxZip)) -- sql is telling me this line has the error
     begin

        insert into #tmpAttorneysFormatted(
            attorneyname,
            zipcode
        )
        select
            attorneyname = @vcrCurrentAttonreyName,
            zipcode = case
                        when len(@vcrMinZip) = 1 then '00' + ltrim(rtrim(@vcrMinZip))
                        when len(@vcrMinZip) = 2 then '0' + ltrim(rtrim(@vcrMinZip))
                        when len(@vcrMinZip) = 3 then ltrim(rtrim(@vcrMinZip))
                      end

        select @vcrMinZip = convert(int, @vcrMinZip) + 1        

     end

    -- this statement does not get hit
    update #tmpAttorneyImport
    set
        parsed = 1
    where
        ident = @intCurrentIdent

 end
else
 begin

    insert into #tmpAttorneysFormatted(
        attorneyname,
        zipcode
    )
    select
        attorneyname = @vcrCurrentAttonreyName,
        zipcode = case
                    when len(@vcrCurrentZip) = 1 then '00' + ltrim(rtrim(@vcrCurrentZip))
                    when len(@vcrCurrentZip) = 2 then '0' + ltrim(rtrim(@vcrCurrentZip))
                    when len(@vcrCurrentZip) = 3 then ltrim(rtrim(@vcrCurrentZip))
                  end

        update #tmpAttorneyImport
        set
            parsed = 1
        where
            ident = @intCurrentIdent

 end

2 个答案:

答案 0 :(得分:1)

说明:

  1. 最有可能@vcrMinZip数据类型为`[VAR] CHAR(3)
  2. 如果@vcrMinZip变量的值为'999',则问题将从select @vcrMinZip = convert(int, @vcrMinZip) + 1行开始,因为SELECT @vcrMinZip = convert(int, '999') + 1表示SELECT @vcrMinZip = 999 + 1 -- = 1000
  3. 因此,您希望将4位INT(1000)值转换为VARCHAR(3)变量。这意味着SQL Server必须截断结果。在这种情况下,而不是截断SQL Server将给出另一个结果:*(“* =结果长度太短而无法显示”) 有关更多信息,请参阅“MSDN”中的Truncating and Rounding Results部分。
  4. 示例:

    DECLARE  @vcrMinZip VARCHAR(3);
    SELECT @vcrMinZip = CONVERT(INT, '999') + 1;
    SELECT  @vcrMinZip;
    
    Results
    
    ----
    *
    
    (1 row(s) affected)
    

    你能做什么?

    • 使用INTSMALLINT代表@vcrMinZip@vcrMaxZip
    • 将所有convert(int, @vcrMinZip)convert(int, @vcrMaxZip)次转换替换为@vcrMinZip@vcrMaxZip
    • zipcode = case ... when .... end替换为更简单的内容:zipcode = RIGHT('000000000'+CONVERT(VARCHAR(10),@vcrMinZip), 3) -- 3 = how many chars do you need

答案 1 :(得分:0)

select @vcrMinZip = ltrim(rtrim(left(@vcrCurrentZip, 3)))
select @vcrMaxZip = ltrim(rtrim(right(@vcrCurrentZip, 3)))

您对数据的清洁程度有多确定?

我放入(在此之后)两行:

print @vcrMinZip
print @vcrMaxZip

并查看字符串中实际被解析的内容。