Wolfgang Chien's Homepage | Delphi學習筆記 - 問答篇 |
請教各位高手:
在使用動態的 SQL 查詢時無法將欄位部份用變數來取代.
SELECT * FROM ANTH WHERE AGE between 20 and 65 ORDER BY :field; ^^^^^^ 這關過不了!
我想要作的是在 SQL 中任意選擇一欄位並排序輸出.請問各位有辦法嗎?
我目前系統採用的方法是不用參數, 而是以 TQuery.SQL.Add()方法在程式執行階段逐一組成 SQL 述句, 以下的程式片斷雖然沒有用到 :Param 這樣的參數, 但個人以為卻比參數更有彈性,提供給您參考:
... (* 開始組合條件值並執行 SQL, 並考慮例外狀況處理 *) with qryMainData do begin (* qryMainData 是一個 TQuery *) try Close; with Sql do begin Clear; Add('Select ph_Date, ph_No from "PLAN_H.DBF" '); (* 組合 SQL 述句 *) if not EmptyStr(sPh_noQ) then sWhere := sWhere + 'ph_no like "' + sPh_noQ + '%" and '; if sPh_DateQBegin[1] <> ' ' then sWhere := sWhere + 'ph_Date >= "' + sPh_DateQBegin + '" and '; if sPh_DateQEnd[1] <> ' ' then sWhere := sWhere + 'ph_Date <= "' + sPh_DateQEnd + '" and '; if sWhere <> '' then sWhere := 'Where ' + LeftStr(sWhere, length(sWhere) - 5); { ShowMessage(sWhere); } Add(sWhere); if sOrderByWho = 'PH_NAME' then Add(' Order by Ph_Name') else Add(' Order By Ph_No'); end; Open; except ShowMessage('無法查出資料'); Exit; end; ...
首頁 | 學習筆記 | 主題公園 | 軟體下載 | 關於本站 | 討論信群 | 相約下次 |