2015-05-24 62 views
0

我有下面的函數,它合併了兩個具有相同字段但數據不同的表,然後在一些字段上執行內部聯接。 但是,我收到了「ID附近'不正確的語法'id',在UNION ALL之後。我很努力地看到我的語法不正確。在SQL中使用聯合和內部聯接

IF (@booking_id is NULL AND @id is NULL) 
    BEGIN 

    SELECT 

    A.id, 
    A.booking_id, 
    A.todo_type, 
    A.due, 
    A.urgent, 
    A.status, 
    A.comment, 
    A.create_user, 
    A.create_date, 
    A.delete_date, 
    B.first_name, 
    B.last_name, 
    H.holiday_name, 
    H.date_start, 
    H.date_end, 
    TT.lookup_desc todo_type_desc, 
    TS.lookup_desc status_desc 

    FROM 

    (
     SELECT 
     id, booking_id, todo_type, due, urgent, status, comment, create_user, create_date, delete_date 
     FROM 
     [TODO] 
     UNION ALL 
     id, booking_id, todo_type, due, urgent, status, comment, create_user, create_date, delete_date 
     FROM 
     [TODO_OPERATOR] 
    ) 

    A 

    INNER JOIN LOOKUP TT ON A.todo_type=TT.lookup_id 
    INNER JOIN LOOKUP TS ON A.status=TS.lookup_id 
    INNER JOIN BOOKING B ON A.booking_id=B.booking_id 
    INNER JOIN HOLIDAY_REF H ON B.holiday_id=H.holiday_id 

    WHERE A.delete_date is NULL and TS.lookup_desc<>'Closed' 
    ORDER BY A.create_date 
    END 
+0

這看起來像SQL Server的代碼,所以我加了標籤。 –

回答

2

你還要添加SELECTUNION ALL

SELECT 
    id, booking_id, todo_type, due, urgent, status, comment, create_user, create_date, delete_date 
    FROM 
    [TODO] 
UNION ALL 
SELECT 
    id, booking_id, todo_type, due, urgent, status, comment, create_user, create_date, delete_date 
    FROM 
    [TODO_OPERATOR] 
+1

* facepalm * !!!!! – PeteTheGreek