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值“ADMIN”转换为数据类型int时,转换失败_Sql_Sql Server - Fatal编程技术网

Sql 将varchar值“ADMIN”转换为数据类型int时,转换失败

Sql 将varchar值“ADMIN”转换为数据类型int时,转换失败,sql,sql-server,Sql,Sql Server,我有下面这样的sql。主页提示: 来自WITH的K表中的结果给出了指定范围的编号代码,例如:1-90 主选择部件在我使用之前工作正常 sum(F._IsInIntersection) [CountIntersection] 它会导致错误 Msg 245,16级,状态1,第11行 将varchar值“ADMIN”转换为数据类型int时,转换失败 如果我评论这行,就可以了 我不知道为什么会发生这个错误,SUM行只包含int值。任何地方都不存在错误中提到的管理员值 有人看到这个查询有什么问题吗 ;w

我有下面这样的sql。主页提示:

来自WITH的K表中的结果给出了指定范围的编号代码,例如:1-90

主选择部件在我使用之前工作正常

sum(F._IsInIntersection) [CountIntersection]
它会导致错误

Msg 245,16级,状态1,第11行 将varchar值“ADMIN”转换为数据类型int时,转换失败

如果我评论这行,就可以了

我不知道为什么会发生这个错误,SUM行只包含int值。任何地方都不存在错误中提到的管理员值

有人看到这个查询有什么问题吗

;with K as ( 
select cast(c.Code as int) code
from rmCategory c, rmCategoryGroup cg, rmIdentification ic
where 1=1
  and c.CategoryGroupID=cg.ID
  and c.ID=ic.ID0
)

select 
F.STAV_AKT [Code]
, count(F.STAV_AKT) [Count] 
, sum(F._IsInIntersection) [CountIntersection]

from
(
select 
    f.*
    , case when f.KVALIF IN (select code from K) and f.KVALIF is not null then 1 else 0 end _IsInIntersection
from frmFormData_208629_MaMo2_IMPORT f, rmIdentification i

where 1=1
    and f.ID=i.id0
    and i.isValid=1
    and f.sysDAVKA=5

) F

group by F.STAV_AKT
order by F.STAV_AKT 

我还尝试在sumcastF内部强制转换参数。\u IsInIntersection为int[CountIntersection],以确保使用int值而不是布尔值,但没有更改。

f.sysDAVKA的数据类型是什么

试着跑步

SELECT f.sysDAVKA 
FROM frmFormData_208629_MaMo2_IMPORT f 
WHERE f.sysDAVKA = 'ADMIN'
我的直觉是你至少会找到一排。如果这是真的,那么将WHERE子句更改为

where 1=1
    and f.ID=i.id0
    and i.isValid=1
    and f.sysDAVKA=CAST(5 AS char(1))

应该可以解决问题。

Martin,我怀疑c.Code在部件中也包含值“ADMIN”

cast(c.Code as int) code
它在没有求和的情况下运行良好,因为它是CTE的一部分,并且在不使用列时不会具体化

例如,如果我在中运行以下代码示例,它运行良好,但如果我也选择了列代码,则在尝试将“ADMIN”强制转换为INT时失败

因此,请尝试使用例如

SELECT DISTINCT Code FROM rmCategory

声明的CTE-“K”未在随后的SELECT语句中使用是,它用于CASE表达式中的子选择F:从KsysDAVKA is int列中选择代码
SELECT DISTINCT Code FROM rmCategory