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

时间:2012-02-26 08:28:32

标签: sql

查询:

SELECT TOP (1) '-1' AS hrempid, 'All employees' AS fullname
FROM employee
UNION
SELECT fullname, hrempid
FROM employee AS employee1 
order by fullname

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

2 个答案:

答案 0 :(得分:4)

union之后,您的列的顺序错误。这是更正版本:

SELECT   TOP (1) '-1' AS hrempid, 'All employees' AS fullname
FROM     employee
UNION
SELECT   hrempid, fullname
FROM     employee AS employee1 
ORDER BY fullname

更好的是,使用这样的东西:

SELECT   CAST(-1 AS BIGINT) AS hrempid, 'All employees' AS fullname
UNION ALL
SELECT   hrempid, fullname
FROM     employee AS employee1 
ORDER BY fullname

答案 1 :(得分:1)

'保留在hrempid周围并切换列。试试这个:

SELECT TOP (1) 'All employees' AS fullname, -1 AS hrempid
FROM employee
UNION
SELECT fullname, hrempid
FROM employee AS employee1 
order by fullname