線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:4748
推到 Plurk!
推到 Facebook!

query的recordcount為-1

答題得分者是:dyming
energybody
一般會員


發表:44
回覆:32
積分:15
註冊:2007-04-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-08-10 14:21:11 IP:211.21.xxx.xxx 訂閱
小弟用 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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-08-10 15:15:30 IP:60.248.xxx.xxx 未訂閱
確認看看是不是(')引號配對的問題?

或者先把全部語法貼在Memo,指定給Query.Sql,再Open看看是否RecordCount還是-1?
VICSYS
初階會員


發表:21
回覆:64
積分:32
註冊:2002-10-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-08-10 16:11:45 IP:219.68.xxx.xxx 未訂閱
如果只是判斷是否有資料時, 不需要用到 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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-08-10 17:52:22 IP:211.72.xxx.xxx 未訂閱
TQuery Open 後加上 First, RecordCount 就正確了, 如果沒資料則 RecordCount = 0, 但是資料如果很多, First 會很慢 (因為要取得所有 Record).
如果 First 不行, 那改用 Last 一定可以 (想也知道).
energybody
一般會員


發表:44
回覆:32
積分:15
註冊:2007-04-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-08-10 17:57:37 IP:211.21.xxx.xxx 訂閱
小弟查大陸的 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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-08-11 16:48:07 IP:211.75.xxx.xxx 訂閱
在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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2007-08-13 17:15:50 IP:59.120.xxx.xxx 未訂閱

依照個人經驗
SELECT 欄位長度大於255,就會出現recordcount=-1
P.D.
版主


發表:603
回覆:4038
積分:3874
註冊:2006-10-31

發送簡訊給我
#8 引用回覆 回覆 發表時間:2007-08-14 00:14:31 IP:61.67.xxx.xxx 未訂閱
使用sql資料庫時,儘量避免使用recordcount 的功能,因為其實電腦必須將資料全部掃一遍才知道有多少筆,如果你使用sql系統在遠端,回傳筆數又很龐大的話,移動指標last是一個很嚴重的做法,勢必造成系統資源損耗嚴重,所以我的做法,要計算筆數,用另一組query 同樣的條件,做 select count(*) ... ,如此回值也快,也不佔太多資源,至於 paradox 為何支援recordcount, 因為它本身 open時就載入所有記錄,自然 recordcount 就已經知道了!
系統時間:2024-05-16 6:51:57
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!