2010-11-27 71 views
0

我有這樣的查詢拋出....分配錯誤在查詢

selectLeaveDetails = 
    "SELECT UL.[PK_ID],UD.FIRST_NAME + ' ' + UD.LAST_NAME AS REQUESTBY," + 
    "UL.[DATE_FROM] AS FROMDATE,UL.[DATE_TO] AS TODATE," + 
    "UL.LEAVE_REQUEST_ON AS REQUESTON," + 
    "REPLACE(UL.LEAVE_REQUEST_NOTES, '\n', '<br />') AS REQUESTNOTES," + 
    "STATUS=CASE " + 
    " WHEN UL.[LEAVE_STATUS] = '1' THEN 'ACTIVE' " + 
    " WHEN UL.[LEAVE_STATUS] = '-1' THEN 'CANCELLED' " + 
    " WHEN UL.[LEAVE_STATUS] = '2' THEN 'REPLACED' END," + 
    "UL.LEAVE_RESPONSE_ON AS RESPONSEON," + 
    "ULL.FIRST_NAME + ' ' + ULL.LAST_NAME AS RESPONSEBY," + 
    "UL.[LEAVE_RESPONSE_NOTES] AS RESPONSENOTES,UL.FK_LEAVE_REQUESTER " + 
    "FROM (M_USER_LEAVES UL " + 
    "INNER JOIN M_LEADERLED MLL ON MLL.LED_ID = MUD.PK_ID WHERE MLL.LEADER_ID = '" + Session["UserID"].ToString() ****" +**** 
    "LEFT JOIN M_USER_DETAILS UD ON UD.PK_ID = UL.FK_LEAVE_REQUESTER) " + 
    "LEFT JOIN M_USER_DETAILS ULL ON ULL.PK_ID = UL.FK_LEAVE_RESPONSE_BY " + 
    " WHERE UL.DATE_FROM BETWEEN '01/01/" + cmbYearList.SelectedItem.Text + "' AND '12/31/" + cmbYearList.SelectedItem.Text + "'" + 
    " AND UD.ACTIVE=1"; 

cmbYearList.SelectedItem.Text + "' AND '12/31/" + cmbYearList.SelectedItem.Text + "'"查詢......只有賦值,遞增,遞減引發錯誤

誰能幫助我?

+0

幾點要點:既然你把你的查詢連接在一起,你就打開了自己的SQL注入 - 不要這樣做!改用參數化查詢。另外:對於日期格式,使用「01/01/...」和「12/31/...」會使您的代碼依賴於SQL Server上的語言設置 - 使用ISO-8601格式替代「YYYYMMDD」 - 那些**無論您的語言/地區設置如何,**總是** – 2010-11-27 09:57:43

回答

1

你FROM子句中不知何故相當錯位了:

FROM (M_USER_LEAVES UL 
     INNER JOIN M_LEADERLED MLL ON MLL.LED_ID = MUD.PK_ID 
     WHERE MLL.LEADER_ID = 'XXXX" 
     LEFT JOIN M_USER_DETAILS UD ON UD.PK_ID = UL.FK_LEAVE_REQUESTER) 

你有一個INNER JOIN,然後WHERE條款,隨後LEFT JOIN ....這似乎很奇怪.....究竟是什麼你想在這裏做?爲什麼你需要把這個變成一個子查詢 - 你不能只將這些表INNER JOINLEFT JOIN合併成一個語句並定義必要的WHERE約束嗎?

此外,在這裏你WHERE條款得到一個開口的單引號和右雙引號 - 這是行不通的......

WHERE MLL.LEADER_ID = 'XXXX" 
        *** *** 

你需要讓你的SQL查詢在SQL工作首先將服務器管理工​​作室 - 然後將其轉移到您的C#代碼中。