admin

将varchar值'none'转换为数据类型int时转换失败

sql

我尝试了以下存储过程

ALTER PROCEDURE spvec
AS
SELECT Row_number() OVER (ORDER BY (SELECT 1)) AS 'RNumber'
    ,vf.*
FROM LOGIN ld
    ,vehicle vf
WHERE (ld.fid = vf.fid)
    AND ld.id <> 'NA'
    AND vf.CustID <> 'None'
    AND vf.Simo <> 'None'
ORDER BY Row_number() OVER (ORDER BY (SELECT 1))

没有,并且行中没有NA文本。例如,这里不为空,但其中的值都不为,NA在此处(ld.fid = vf.fid)

更新

ld.fid是字符串,而vf.fid是int

所以我尝试转换

ALTER PROCEDURE spvec
AS
SELECT Row_number() OVER (ORDER BY (SELECT 1)) AS 'RNumber'
    ,vf.*
FROM LOGIN ld
    ,vehicle vf
WHERE  CAST((ld.FFID=vf.FFID) AS INT)
    AND ld.id <> 'NA'
    AND vf.CustID <> 'None'
    AND vf.Simo <> 'None'
ORDER BY Row_number() OVER (ORDER BY (SELECT 1))\

这显示更新时出现以下错误

关键字“ AS”附近的语法不正确。

‘)’附近的语法不正确。

它会引发以下错误而不进行更新

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


阅读 223

收藏
2021-06-07

共1个答案

admin

如果您的表实际上由None和NA组成,请尝试以下操作:

ALTER PROCEDURE spvec
AS
SELECT Row_number() OVER (ORDER BY (SELECT 1)) AS 'RNumber'
    ,vf.*
FROM LOGIN ld
    ,vehicle vf
WHERE (ld.fid = cast(vf.fid as varchar(max)))
    AND cast(ld.id as varchar(2)) <> 'NA'
    AND cast(vf.CustID as varchar(4)) <> 'None'
    AND cast(vf.Simo as varchar(4)) <> 'None'
ORDER BY Row_number() OVER (ORDER BY (SELECT 1))
2021-06-07