开发者

Conversion failed when converting the varchar value '*' to data type int

开发者 https://www.devze.com 2023-04-10 12:38 出处:网络
I\'m getting this issue when i\'m running nested while loops in sql server 2005. My outer loop gets one iteration, and then my inner loop gets it\'s full first iteration, but my statement after the i

I'm getting this issue when i'm running nested while loops in sql server 2005.

My outer loop gets one iteration, and then my inner loop gets it's full first iteration, but my statement after the inner loop never gets executed, which then seems to break everything.

I'm lost right now and I feel like I'm missing something very easy, any help is much appreciated.

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 = ltr开发者_如何学Pythonim(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

end


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

How sure are you that your data is clean?

I'd put in (right after this) two lines:

print @vcrMinZip
print @vcrMaxZip

and see what is actually being parsed out of the string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号