Autor Beitrag
Mysterio08
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Mi 22.09.10 16:00 
Hallo,

ich suche eine Funktion, welche mit als Rückgabewert sagt, ob es in einer Spalte einer Datenbanktabelle einen Eintrag mit dem Wert "Beispielwert" gibt. Das ist mein Ansatz:
DataSource2.DataSet.FieldByName('AuftragID').?
Natürlich könnte ich auch einfach eine SQL-Abfrage starten und prüfen, ob die Anzahl der gefundenen Aufträge > 0 ist, aber mit einer Funktion wäre es doch schöner!

mfg.
Tankard
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Administrator
Beiträge: 217
Erhaltene Danke: 96



BeitragVerfasst: Mi 22.09.10 16:08 
mit

select * from auftrag where aufrtagsid=XXXXXX

dann wie folgt:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
dataset.open;
dataset.first;
if dataset.eof then
  begin
    //nix da
  end
else  // was da
Mysterio08 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Mi 22.09.10 16:15 
Mhm, aber wie genau muss die Anweisung lauten? Ich habe ja nicht nur einen Datensatz, sondern mehrere. Ich habe deinen Ansatz, Tankard, mal so verwendet:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
DataSource2.DataSet.Open; //Muss das explizit "geöffnet" werden?
while not DataSource2.DataSet.Eof do begin
  DataSource2.DataSet.FieldByName('AuftragID').??? then Existiert := True
end;


Edit: Da warst du schneller ;D
Aber ich wollte mich gerade um diese SQL-Anweisung (die ja noch ausgeführt werden muss?) "drücken", weil es auch darum geht, eine neue SQL-Abfrage zu starten, um die Datensätze im TDBGrid wieder anzuzeigen, oder geht das nicht verloren?
Tankard
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Administrator
Beiträge: 217
Erhaltene Danke: 96



BeitragVerfasst: Mi 22.09.10 16:18 
ahso meintest du das

schau dir mal locate, lookup, bzw seek an.

hier ne artikel zu den funktionen:

delphi.about.com/od/...base/l/aa052901a.htm

gruss
tankard
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Mi 22.09.10 16:25 
Ich würde das ganze einmalig wegkapseln ....

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Procdure GetHelpQuery(Ads:Tadodataset;const SQL:String);
begin
   Ads.Close;
   Ads.Commandtext := SQL;
   Ads.Open;
end;

Function GetCountForFieldAndValue(Ads:Tadodataset;const Table,Field,Value:String):Integer;
begin
  GetHelpQuery(Ads,'Select Count(*) from ' + Table + ' where ' + Field + '=' + Value);
  Result := Ads.Fields[0].asInteger;
end;
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 23.09.10 07:49 
Wenn auf der ID ein Index liegt, wovon ich mal ausgehe, würde ich ein SQL-Abfrage machen.
Ein Locate verschiebt meines Wissens auch den Datensatzzeiger, was oft zu unerwünschten Nebeneffekten führt.

Also einfach ein weiteres Dataset ezeugen und eine passende SQL-Abfrage definieren.
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Do 23.09.10 08:12 
Alternativ zu .Locate() gäbe es noch .Lookup(), welches den Datensatzzeiger nicht verschiebt.

_________________
Markus Kinzler.
Mysterio08 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Fr 24.09.10 17:01 
Danke, das hat mir alles geholfen, ich habe es jetzt hinbekommen!