转换 varchar 值“*”时转换失败数据类型为 int

发布于 2024-12-07 20:06:16 字数 2339 浏览 0 评论 0原文

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

结尾

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 = 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

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-12-14 20:06:16
select @vcrMinZip = ltrim(rtrim(left(@vcrCurrentZip, 3)))
select @vcrMaxZip = ltrim(rtrim(right(@vcrCurrentZip, 3)))

您如何确定您的数据是干净的?

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

print @vcrMinZip
print @vcrMaxZip

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文