Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将数据类型varchar转换为bigint时出错。使用更新时_Sql_Sql Server - Fatal编程技术网

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

我正在使用SQL Server。我正在尝试运行以下SQL脚本,但出现以下错误:

将数据类型varchar转换为bigint时出错

以下是脚本:

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')
是正确的!!!