SELECT INTO语句失败

时间:2014-04-23 06:08:18

标签: sql-server-2008

你们可以指导我为什么这不起作用吗?

SELECT AN.an_id, AN.an_name, 
    (SELECT TOP(1)ex_date
        FROM upd_exam_headers
        WHERE HS.an_id = AN.an_id
        ORDER BY ex_date desc), 
        sum(ex_fee)
INTO upd_nc_felines AS FS
FROM upd_animals AS AN
LEFT JOIN upd_exam_headers AS HS ON HS.an_id = AN.an_id
LEFT JOIN upd_exam_details AS DS ON DS.ex_id = HS.ex_id
WHERE an_type = 'cat' and an_status = 'NC'
GROUP BY AN.an_id, AN.an_name;

第15行,第15行,第1行,第10行 关键字' AS'附近的语法不正确。

2 个答案:

答案 0 :(得分:3)

错误消息说明含义:)

sum(ex_fee)

添加别名

答案 1 :(得分:1)

试试这个,希望应该有效,

WITH TMP AS (
SELECT 
AN.an_id, AN.an_name, 
(SELECT TOP 1 ex_date         
FROM upd_exam_headers
WHERE HS.an_id = AN.an_id
ORDER BY ex_date desc) AS 'ex_date', 
sum(ex_fee) AS 'ex_fee'

FROM upd_animals AS AN
LEFT JOIN upd_exam_headers AS HS ON HS.an_id = AN.an_id
LEFT JOIN upd_exam_details AS DS ON DS.ex_id = HS.ex_id
WHERE an_type = 'cat' and an_status = 'NC'
GROUP BY AN.an_id, AN.an_name;

)
SELECT * INTO
upd_nc_felines
FROM TMP