转换 varchar 值“*”时转换失败数据类型为 int
当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您如何确定您的数据是干净的?
我将(在此之后)放入两行:
并查看实际从字符串中解析出的内容。
How sure are you that your data is clean?
I'd put in (right after this) two lines:
and see what is actually being parsed out of the string.