2015-11-04 73 views
0

我很努力與MS Access 2010 SQL。任何人都可以看到爲什麼下面的SQL語句會在FROM子句中產生「語法錯誤?」SQL聯盟所有「語法Eror in從句子」

INSERT INTO Attendance 
SELECT [AttendanceData].Student, [AttendanceData].[10/1/2015] AS AttendValue, 
"10/1/15" AS AttendDate 
FROM [AttendanceData] 
UNION ALL 
SELECT [AttendanceData].Student, [AttendanceData].[10/2/2015] AS AttendValue, 
"10/2/15" As AttendDate 
FROM [AttendanceData]; 

個人陳述做工精細,但與UNION ALL結合,我收到

語法錯誤在FROM子句「。

任何幫助,將不勝感激!

謝謝

+0

什麼是'Attendance'表中的列? –

+0

請參閱['FROM'與巨大的UNION子句有關的語法錯誤](http://stackoverflow.com/questions/17284174/from-syntax-error-involved-with-huge-union-clause) – Marusyk

+0

並且使用適當的字符串表達式爲您的日期值:'#2015/10/2#作爲AttendDate' – Gustav

回答

1

嘗試使用某些東西這樣的:

INSERT INTO Attendance 
SELECT R.Student, R.AttendValue, R.AttendDate 
FROM 
(
    (SELECT [AttendanceData].Student, [AttendanceData].[10/1/2015] AS AttendValue, 
    "10/1/15" AS AttendDate 
    FROM [AttendanceData]) 

    UNION ALL 

(SELECT [AttendanceData].Student, [AttendanceData].[10/2/2015] AS AttendValue, 
    "10/2/15" As AttendDate 
    FROM [AttendanceData]) 
) AS R 
0

在MS Access的Jet/ACE SQL,你不能在追加查詢使用聯合查詢直接。您將看到查詢調試器突出顯示UNION ALL語句。

將聯合查詢單獨保存爲存儲的查詢對象,然後將其用於追加查詢的SELECT語句中。

INSERT INTO Attendance 
SELECT * FROM UnionAttendance 

或者,你可以使用派生表:

INSERT INTO Attendance 
SELECT * 
FROM ( 
    SELECT [AttendanceData].Student, [AttendanceData].[10/1/2015] AS AttendValue, 
    "10/1/15" AS AttendDate 
    FROM [AttendanceData] 
    UNION ALL 
    SELECT [AttendanceData].Student, [AttendanceData].[10/2/2015] AS AttendValue, 
    "10/2/15" As AttendDate 
    FROM [AttendanceData] 
);