2013-10-24 106 views
2

有什麼不對下面的SQL代碼:SQL訪問錯誤 - 在查詢表達式語法錯誤

INSERT INTO tbl_HAMP_Actions (Loan_ID, Action_ID, Event_User, Event_Time, Event_Station, Reason_ID) 
VALUES(3238, 3, 'CONTROL', Now(), 'CONTROL', (SELECT tbl_Reasons.Reason_ID 
FROM tbl_Reasons 
WHERE tbl_Reasons.Reason = 'Incorrect Information')); 
+0

請添加您收到任何錯誤消息。 – Matt

回答

0

我不認爲你可以使用子查詢作爲值的訪問。 試試這個:

INSERT INTO tbl_HAMP_Actions (Loan_ID, Action_ID, Event_User, Event_Time, Event_Station, Reason_ID) 
SELECT 3238, 3, 'CONTROL', Now(), 'CONTROL', Reason_ID 
FROM tbl_Reasons 
WHERE tbl_Reasons.Reason = 'Incorrect Information' 
+0

謝謝尤里。它工作完美。 –

+0

很高興爲你效勞:)如果解決方案有用,請檢查答案是否被接受。 –

0
(SELECT tbl_Reasons.Reason_ID 
FROM tbl_Reasons 
WHERE tbl_Reasons.Reason = 'Incorrect Information') 

可以將代碼返回多行多有關係嗎?如果是的話,這將失敗。

0

我想你可能有更好的運氣有一個查詢,看起來像這樣:

INSERT INTO tbl_HAMP_Actions 
    (
     Loan_ID, 
     Action_ID, 
     Event_User, 
     Event_Time, 
     Event_Station, 
     Reason_ID 
    ) VALUES (
     3238, 
     3, 
     'CONTROL', 
     Now(), 
     'CONTROL', 
     DLookup("Reason_ID","tbl_Reasons","Reason = 'Incorrect Information'") 
    )