query的recordcount為-1 |
答題得分者是:dyming
|
energybody
一般會員 發表:44 回覆:32 積分:15 註冊:2007-04-12 發送簡訊給我 |
小弟用 query 來查詢資料, 有用 recordcount 來檢查 record, 結果 recordcount 卻出現 -1, 照道理講 recordcount 應該 >=0 才對, 用 SQL server 來查詢結果有資料, 請問是哪裏出現問題.
qryExcel.close; qryExcel.SQL.clear; qryExcel.SQL.add('select a.*,b.job,b.eta,b.onboard,c.fwd from ibl a, ijob b, agnt c '); qryExcel.SQL.Add('where a.fn = b.fn '); qryExcel.SQL.Add('and b.agnt = c.code ' ); qryExcel.SQL.Add('and (a.split in (''.'',''*'')) '); if cbFWD.ItemIndex>0 then qryExcel.SQL.Add('and c.fwd = ''' cbFWD.text ''' '); if ckBy.checked then qryExcel.SQL.Add('and b.' cbPort.text ' = ''' edtByPort.text ''' '); if rbETA.Checked then begin qryExcel.SQL.add('and b.eta >= ''' edtSdate.text ''' '); qryExcel.SQL.add('and b.eta <= ''' edtEdate.text ''' '); end else begin qryExcel.SQL.add('and b.onboard >= ''' edtSdate.text ''' '); qryExcel.SQL.add('and b.onboard <= ''' edtEdate.text ''' '); end; if cbonly.Checked then qryExcel.SQL.add('and a.tradetype=''R'' '); if cbexrot.Checked then qryExcel.SQL.add('and a.tradetype<>''R'' '); if edtssales.Text<>'' then qryExcel.SQL.Add('and a.sales=''' edtssales.text ''' '); case pos(cbLOC.text,'TPE-TCH-KAO-KHH') of 1: qryExcel.SQL.Add('and a.sales in (select code from sales where location=''T'') '); 5: qryExcel.SQL.Add('and a.sales in (select code from sales where location=''C'') '); 9,13: qryExcel.SQL.Add('and a.sales in (select code from sales where location=''K'') '); end; qryExcel.SQL.Add('order by a.sales,b.job'); test:=qryexcel.Text; <-- 檢查 qryexcel 的語法也正確, 用在 SQL Server 也出現資料 qryExcel.open; showmessage(Inttostr(qryexcel.recordcount)); <-- showmessage 出現 -1 if qryExcel.RecordCount=0 then <-- trace 時也出現 -1 begin showmessage('查無資料,請重設條件!'); exit; end; |
pedro
尊榮會員 發表:152 回覆:1187 積分:892 註冊:2002-06-12 發送簡訊給我 |
|
VICSYS
初階會員 發表:21 回覆:64 積分:32 註冊:2002-10-10 發送簡訊給我 |
如果只是判斷是否有資料時, 不需要用到 recordcount
而且像 SQLQuery 這樣的 "單向性" DataSet, 並沒有把資料放入 Buffer 所以 recordcount 時, 會執行 select count(*) from ... 這應該也不是你要的吧! 我在判斷是否有資料時, 是找一個必要輸入的欄位, 然後用 TField.IsNul 來判斷, 或者是用 DataSet IsEmpty
編輯記錄
VICSYS 重新編輯於 2007-08-10 16:18:52, 註解 加上 IsEmpty‧
|
dyming
初階會員 發表:0 回覆:11 積分:32 註冊:2003-04-21 發送簡訊給我 |
|
energybody
一般會員 發表:44 回覆:32 積分:15 註冊:2007-04-12 發送簡訊給我 |
小弟查大陸的 delphi 文章, 他們說是 delphi & BDE 的錯誤的問題, 說 recordcount 只能用在 dBase, Paradox 才會正確, 不然有時會不正確,
解決的方法是在 qryExcel.open 後, 加入 qryExcel.Last 和 qryExcel.First 這樣 recordcount 就會正確, 小弟有試過就不會出現, recordcount=-1 的錯誤. ===================引 用 energybody 文 章=================== 小弟用 query 來查詢資料, 有用 recordcount 來檢查 record, 結果 recordcount 卻出現 -1, 照道理講 recordcount 應該 >=0 才對, 用 SQL server 來查詢結果有資料, 請問是哪裏出現問題. ? qryExcel.close; ? qryExcel.SQL.clear; ? qryExcel.SQL.add('select a.*,b.job,b.eta,b.onboard,c.fwd from ibl a, ijob b, agnt c '); ? qryExcel.SQL.Add('where a.fn = b.fn '); ? qryExcel.SQL.Add('and b.agnt = c.code ' ); ? qryExcel.SQL.Add('and (a.split in (''.'',''*'')) '); ? if cbFWD.ItemIndex>0 then ???? qryExcel.SQL.Add('and c.fwd = ''' cbFWD.text ''' '); ? if ckBy.checked then ???? qryExcel.SQL.Add('and b.' cbPort.text ' = ''' edtByPort.text ''' '); ? if rbETA.Checked then ? begin ??? qryExcel.SQL.add('and b.eta >= ''' edtSdate.text ''' '); ??? qryExcel.SQL.add('and b.eta <= ''' edtEdate.text ''' '); ? end else ? begin ??? qryExcel.SQL.add('and b.onboard >= ''' edtSdate.text ''' '); ??? qryExcel.SQL.add('and b.onboard <= ''' edtEdate.text ''' '); ? end; ? if cbonly.Checked then ??? qryExcel.SQL.add('and a.tradetype=''R'' '); ? if cbexrot.Checked then ??? qryExcel.SQL.add('and a.tradetype<>''R'' '); ? if edtssales.Text<>'' then ??? qryExcel.SQL.Add('and a.sales=''' edtssales.text ''' '); ? case pos(cbLOC.text,'TPE-TCH-KAO-KHH') of ??? 1: qryExcel.SQL.Add('and a.sales in (select code from sales where location=''T'') '); ??? 5: qryExcel.SQL.Add('and a.sales in (select code from sales where location=''C'') '); ??? 9,13: qryExcel.SQL.Add('and a.sales in (select code from sales where location=''K'') '); ? end; ? qryExcel.SQL.Add('order by a.sales,b.job'); ? test:=qryexcel.Text;?<-- 檢查 qryexcel 的語法也正確, 用在 SQL Server 也出現資料??????? ? qryExcel.open; ? showmessage(Inttostr(qryexcel.recordcount));?<-- showmessage 出現 -1 ? if qryExcel.RecordCount=0 then?<-- trace 時也出現 -1 ? begin ??? showmessage('查無資料,請重設條件!'); ??? exit; ? end; |
eaglewolf
資深會員 發表:4 回覆:268 積分:429 註冊:2006-07-06 發送簡訊給我 |
在Help中早有說明:
Note: Use RecordCount with care, because record counting can be a costly operation, especially for SQL queries that return large result sets. Generally, an application should only use RecordCount with Paradox and dBASE tables.
------
先查HELP 再查GOOGLE 最後才發問 沒人有義務替你解答問題 在標題或文章中標明很急 並不會增加網友回答速度 Developing Tool: 1.Delphi 6 2.Visual Studio 2005 3.Visual Studio 2008 DBMS: MS-SQL |
No-where
一般會員 發表:31 回覆:53 積分:16 註冊:2002-09-05 發送簡訊給我 |
|
P.D.
版主 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
使用sql資料庫時,儘量避免使用recordcount 的功能,因為其實電腦必須將資料全部掃一遍才知道有多少筆,如果你使用sql系統在遠端,回傳筆數又很龐大的話,移動指標last是一個很嚴重的做法,勢必造成系統資源損耗嚴重,所以我的做法,要計算筆數,用另一組query 同樣的條件,做 select count(*) ... ,如此回值也快,也不佔太多資源,至於 paradox 為何支援recordcount, 因為它本身 open時就載入所有記錄,自然 recordcount 就已經知道了!
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |