Sql 将数据类型varchar转换为bigint时出错。使用更新时
我正在使用SQL Server。我正在尝试运行以下SQL脚本,但出现以下错误: 将数据类型varchar转换为bigint时出错 以下是脚本:Sql 将数据类型varchar转换为bigint时出错。使用更新时,sql,sql-server,Sql,Sql Server,我正在使用SQL Server。我正在尝试运行以下SQL脚本,但出现以下错误: 将数据类型varchar转换为bigint时出错 以下是脚本: with T as ( select sp.ProfileId ,sp.ProfileHandle ,sp.[Description] from [SocialProfile] sp inner join Entity e on sp.Entity
with T as
(
select
sp.ProfileId
,sp.ProfileHandle
,sp.[Description]
from
[SocialProfile] sp
inner join
Entity e on sp.EntityId = e.EntityId
where
e.EntityStatusId != 3 and e.EntityStatusId != 4
and sp.SocialProfileTypeId in (1, 2, 10)
and (ISNUMERIC(sp.ProfileHandle) = 1)
and IsRemoved = 0
)
update T
set ProfileHandle = NULL
where ProfileHandle = ProfileId
我试图使用cast函数,但它不起作用。有人能告诉我我做错了什么吗?这个isnumeric()
不能提供您想要的保护。使用try\u convert()
进行比较:
with T as (
select sp.ProfileId, sp.ProfileHandle, sp.[Description]
from [SocialProfile] sp inner join
Entity e
on sp.EntityId = e.EntityId
where e.EntityStatusId not in (3, 4) and
sp.SocialProfileTypeId in (1, 2, 10) and
ISNUMERIC(sp.ProfileHandle) = 1 and -- you can leave it in
IsRemoved = 0
)
update T
set ProfileHandle = NULL
where try_convert(int, ProfileHandle) = ProfileId;
SQL Server有一个“功能”,它将重新排列查询中的条件。CTE不会“先”执行,因此isnumeric()。我认为这是一个错误。Microsoft认为这是一项功能(因为它提供了更多优化选项)
查询中保证的唯一顺序是通过case
表达式。最简单的解决方法是尝试转换()
此外,我强烈建议不要依赖隐式转换。始终显式转换。我花了很多时间调试代码,以解决隐式转换引起的问题。ProfileId可能是varchar类型。。使用转换函数导致错误的值是什么?记住ISNUMERIC('123.45,6')
和ISNUMERIC('123,4.56')
是正确的!!!