2014-10-06 158 views
1

我無法弄清楚這一點。「SELECT」附近的SQLException語法不正確

這個查詢運行SQLSERVER中沒有錯誤:

SELECT  COUNT(tjmm.MAINMISSION_PARTICIPANT_SID) 
FROM   T_MOVEMENT_LEG ml INNER JOIN 
       TJ_MVLG_PARTMOV tjmp ON ml.MOVEMENTLEG_SID = tjmp.MOVEMENTLEG_SID INNER JOIN 
       T_MAIN_MISSION mm INNER JOIN 
       T_MISSION_PARTICIPANTS mp ON mm.DETCO_MISSIONPARTICIPANT_SID = mp.MISSION_PARTICIPANT_SID INNER JOIN 
       T_PARTICIPANT_MOVEMENT pm ON mp.MISSION_PARTICIPANT_SID = pm.MISSION_PARTICIPANT_SID INNER JOIN 
       TJ_MMIS_MISPART tjmm ON mm.MAINMISSION_SID = tjmm.MAINMISSION_SID AND 
       mp.MISSION_PARTICIPANT_SID = tjmm.MISSION_PARTICIPANT_SID ON 
       tjmp.PARTICIPANTMOVEMENT_SID = pm.PARTICIPANTMOVEMENT_SID 
WHERE   (mm.IN_USE = 1) AND (mp.IS_OPS = 1) AND (pm.IS_OPS = 1) AND 
       (ml.LEGEFFECTIVE_STARTDATE < { fn NOW() }) AND (ml.LEGEFFECTIVE_ENDDATE > { fn NOW() }) AND 
       (ml.IS_CANCELED = 0) 

它轉化到@Formula:

@Formula("select count(tjmm.MAINMISSION_PARTICIPANT_SID) " 
     + "FROM T_MOVEMENT_LEG ml INNER JOIN " 
     + "TJ_MVLG_PARTMOV tjmp ON ml.MOVEMENTLEG_SID = tjmp.MOVEMENTLEG_SID INNER JOIN " 
     + "T_MAIN_MISSION mm INNER JOIN " 
     + "T_MISSION_PARTICIPANTS mp ON mm.DETCO_MISSIONPARTICIPANT_SID = mp.MISSION_PARTICIPANT_SID INNER JOIN " 
     + "T_PARTICIPANT_MOVEMENT pm ON mp.MISSION_PARTICIPANT_SID = pm.MISSION_PARTICIPANT_SID INNER JOIN " 
     + "TJ_MMIS_MISPART tjmm ON mm.MAINMISSION_SID = tjmm.MAINMISSION_SID AND " 
     + "mp.MISSION_PARTICIPANT_SID = tjmm.MISSION_PARTICIPANT_SID ON " 
     + "tjmp.PARTICIPANTMOVEMENT_SID = pm.PARTICIPANTMOVEMENT_SID " 
     + "WHERE (mm.IN_USE = 1) AND (mp.IS_OPS = 1) AND (pm.IS_OPS = 1) AND " 
     + "tjmm.MainMission_SID = MainMission_SID AND" 
     + "(ml.LEGEFFECTIVE_STARTDATE < NOW()) AND (ml.LEGEFFECTIVE_ENDDATE > NOW()) AND " 
     + "(ml.IS_CANCELED = 0)") 
private Integer opsCount; 

改變了NOW()並添加鏈接到該POJO編號:tjmm.MainMission_SID = MainMission_SID

任何人都知道我在公式中有什麼缺點?

+3

我看到你在年底錯過了空間問題:+「tjmm.MainMission_SID = MainMission_SID AND」 – Pienterekaak 2014-10-06 15:31:10

+0

@Pienterekaak aargg,無法測試到明天但這確實是一個很好的候選人,謝謝 – chillworld 2014-10-06 15:32:38

回答

3

簡單地說:包裹@Formula的用大括號

// instead of this 
// @Formula("select count(tjmm.MAINMISSION_PARTICIPANT_SID) " 
// use this 
@Formula("(select count(tjmm.MAINMISSION_PARTICIPANT_SID) " 
      ... 
     ")") 

更長故事內容:所述@Formula映射,表示表述 「柱狀」。這裏記載:

它可以包含導致選擇這樣的陳述:

SELECT 
    column1,   
    column1 + column2, // expecting columns contain number 
    column1 * column2, 
    ... 

但是,這是行不通的

SELECT 
    column1,   
    ... 
    SELECT count(*) FROM OtherTable, // this won't work 

因爲這樣聲明必須用大括號

包裝
SELECT 
    column1,   
    ... 
    (SELECT count(*) FROM OtherTable), // working while wrapped with() 

這與@Formula上述