将varchar转换为bigint时出错

时间:2016-10-12 13:48:05

标签: sql-server common-table-expression

我正在执行以下查询。在查询中,ChID不是Cat表的列。为此,我将0作为ChID放置并将其转换为bigint数据类型以匹配CatAtt的列的数据类型。但我收到以下错误:

  

Msg 8114,Level 16,State 5,Line 1
  将数据类型varchar转换为bigint时出错。

我在Oracle中执行了与Oracle相关更改的类似查询,我得到了结果。

WITH rg (ID, ChID, Name) AS 
(
    SELECT
        ID,
        CAST(0 AS bigint) ChID,
        Name
    FROM Cat
    WHERE ID = 1000

    UNION ALL

    SELECT
        c.ID,
        c.ChID,
        k.Name
    FROM CatAtt c
    JOIN rg p ON (c.ChID = p.ID)
    JOIN Cat k ON (c.ID = k.ID)
)
SELECT DISTINCT
    ka.ID
FROM 
    rg, Parent app
JOIN 
    Cat ka ON (ka.ID = app.UserIdentifier)
JOIN 
    Owner dt ON (dt.ID = app.ID)
WHERE 
    app.ID = 25773
    AND app.aprroved = 0
    AND REPLACE(ka.Name, ka.Type + '_', dt.OwnerID + '_') = rg.Name

1 个答案:

答案 0 :(得分:-1)

尝试转换而不是强制转换

WITH rg (ID, ChID, Name) AS 
(
SELECT
    ID,
    CONVERT(bigint , '0') ChID,
    Name
FROM Cat
WHERE ID = 1000

UNION ALL

SELECT
    c.ID,
    c.ChID,
    k.Name
FROM CatAtt c
JOIN rg p ON (c.ChID = p.ID)
JOIN Cat k ON (c.ID = k.ID)
)
SELECT DISTINCT
ka.ID
FROM 
rg, Parent app
JOIN 
Cat ka ON (ka.ID = app.UserIdentifier)
JOIN 
Owner dt ON (dt.ID = app.ID)
WHERE 
app.ID = 25773
AND app.aprroved = 0
AND REPLACE(ka.Name, cast((ka.Type) as nvarchar(max)) + '_', cast((dt.OwnerID as nvarchar(max)) + '_')) = rg.Name