如下這段代碼怎麼傳遞動態參數比較合適?(問題有點長~謝謝) Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiDB/html/delphi_20061224164816130.html
原始碼如下(不用細看):
代碼在SQL的查詢分析器中運行是成功的,改動文法方面的細節後得到以下代碼!
select a.company AS company ,b.total_suttle AS last_left,this_in ,this_p ,this_t ,e.total_suttle AS this_left
from gongsi a
left join
(
SELECT kehe,total_suttle
FROM
(
SELECT kehe as lcompany,maxdate=MAX(c_date)
FROM storagetotal
WHERE c_date<=:starttime
GROUP BY kehe
)Kehe_and_Date,storagetotal
WHERE kehe=lcompany AND c_date=maxdate
) b
on a.company=b.kehe
left join
(
SELECT kehe,SUM(s_weight) AS this_in
FROM total
WHERE s_time BETWEEN :starttime and :endtime
GROUP BY kehe
)c
on a.company=c.kehe
left join
(
SELECT kehe,SUM(p_weight) AS this_p,SUM(t_weight) AS this_t
FROM total
WHERE alter_time BETWEEN :starttime and :endtime
GROUP BY kehe
)d
on a.company=d.kehe
left join
(
SELECT kehe,total_suttle
FROM
(
SELECT kehe as lcompany,maxdate=MAX(c_date)
FROM storagetotal
WHERE c_date<=:endtime
GROUP BY kehe
)Kehe_and_Date,storagetotal
WHERE kehe=lcompany AND c_date=maxdate
)e
on a.company=e.kehe
其中的動態參數是:startime和endtime,他們分別來自介面上的兩個TDateTimePicker控制項starttime1和endtime1。
我試過好幾種方法
1、把以上代碼寫到預存程序中,用ADOStoredProc元件連線,然後的的代碼是:
with ADOStoredProc1 do
begin
Paramaters.ParamByName('@starttime').Value=DateTimePicker1.date;
Paramaters.ParamByName('@endtime').Value=DateTimePicker2.date;
ExecProc;
end;
這個方法的結果是程式成功運行,但是在DBGrid中卻什麼都沒有。有人說預存程序可以返回資料集,有人又說不行,到底行還是不行啊?行的話又該如何返回顯示呢?
2、在Object inspector下使用ADOQuery控制項的SQL屬性,直接在其下的List editor下粘貼以上代碼,從其Parameters屬性下看到了六個變數參數
starttime
starttime
endtime
starttime
endtime
endtime
然後我在程式中寫入了
with ADOQuery2 do
begin
Parameters.ParamByName('starttime').Value:=starttime1.date;
Parameters.ParamByName('endtime').Value:=endtime1.date;
ExecSQL;
end;
運行啟動該程式的按鈕後系統報錯,說“不正常的定義參數對象,提供了不一致或不完整的資訊”
然後我又按Parameters屬性中六個參數的排列順序又寫了一遍,即寫了六條之前的傳遞參數值的語句,得到的結果還是一樣!
3、我把以上查詢語句全部以字串形式給一個SQLstr的字元型變數的參數,然後用ADOquery控制項的SQL.Add方法載入,然後運行得到的結果仍然是那樣~
我還試了其他方法,很多時候又報錯說找不到starttime這個參數!
我所以現在的疑問是:這麼長而又複雜的一段select查詢語句,還有動態參數,要用什麼樣的方法來實現最後的顯示資料集的功能比較合適?因為複雜,所以細節都比較瑣碎,不容易注意到,所以能找盡量直接的方法!
希望能得到一些具體的建議吧~因為這個問題困擾了我幾天了,總找不到合適的解決方案~
預存程序能解決的話,用預存程序是比較方便的。但是具體怎麼用呢?
你的問題只有一個:
:starttime 和 :endtime 不能重複使用在同一個adoquery的參數表裡。
處理辦法:
重新命名:
:starttime1~:starttime3
:endtime1~:endtime3
重新賦值
再運行
謝謝,我試一下看看!
果然如你所說!!
萬分感謝了!!
呵呵~~
八十分請您笑納~~