【问题标题】:Conversion failed when converting the varchar value '*' to data type int将 varchar 值“*”转换为数据类型 int 时转换失败
【发布时间】:2011-12-01 03:26:22
【问题描述】:

当我在 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

结束

【问题讨论】:

    标签: sql-server-2005 while-loop


    【解决方案1】:
    select @vcrMinZip = ltrim(rtrim(left(@vcrCurrentZip, 3)))
    select @vcrMaxZip = ltrim(rtrim(right(@vcrCurrentZip, 3)))
    

    您有多确定您的数据是干净的?

    我会输入(紧接着)两行:

    print @vcrMinZip
    print @vcrMaxZip
    

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

    【讨论】:

    • 我已经在这个位置进行了调试,我看到的数据和我期望的一样——在这种情况下,最小值 = 995,最大值 = 999。
    猜你喜欢
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多