Autor Beitrag
csigg
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 706

WIn XP, Win NT, Win2000, Suse8.0
Delphi 5, Delphi 6
BeitragVerfasst: Mo 18.04.05 08:25 
Wie verhindere ich am einfachsten die erstellung eines doppelten Datensatzes??
gibts da noch Funtkion oder soetwas, oder muss ich da erst die alten Daten alle auslesen und dann vergeleichen.
Hab übrigens nee InterBase DB und delphi 5.
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: Mo 18.04.05 08:33 
Mehrere Möglichkeiten (ich gehe mal von SQL aus):
1. Eine DB-Abfrage mit einem passenden SQL-Statement. Damit prüfst du, ob der DS schon existierst.
2. (Meine Empfehlung von heute ohne Nachtisch) Nimm eine Stored-Procedure. Übergebe deine "neuen" Daten dahin. Wenn der DS noch nicht existiert, wird er nicht eingefügt. Basis in der SP wäre dann wieder Punkt 1
3. (für die Harten) Mach einen Unique-Key über den ganzen DS (oder über die Felder, die nicht gleich sein dürfen). Beim Insert bekommst du dann eine Exception :twisted:
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 18.04.05 08:35 
Leg nen Primär-Index auf die ganze Tabelle (ich weiß, dass ich von jedem ERM-ler für diesen Vorschlag gekillt werde)., oder BESSER: Nur auf die Felder, die eindeutig sein müssen.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
csigg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 706

WIn XP, Win NT, Win2000, Suse8.0
Delphi 5, Delphi 6
BeitragVerfasst: Mo 18.04.05 08:40 
es gibt leider kein eindeutiges Feld.
Es gibt eine Tabelle Mitarbeiter(Name, MitarbeiterNr,...) und eine Tabelle Kenntnisse(Name,KentnissNr.). Und dann gibts noch die Tabelle um die es geht, Mitarbeiter Kenntnisse(MitarbeiterNr,KenntnisNr)
csigg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 706

WIn XP, Win NT, Win2000, Suse8.0
Delphi 5, Delphi 6
BeitragVerfasst: Mo 18.04.05 08:44 
@jasocul was wär das für ein passendes Statement??
(Sorry, steht grad ein wenig auf der Leitung)
Amiga-Fan
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 534



BeitragVerfasst: Mo 18.04.05 08:46 
select count(*) as reccount from xxx where xxx; ist vermutlich gemeint

if fieldbyname('reccount').asinteger=0 then begin
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: Mo 18.04.05 08:56 
user profile iconAmiga-Fan hat folgendes geschrieben:
select count(*) as reccount from xxx where xxx; ist vermutlich gemeint

if fieldbyname('reccount').asinteger=0 then begin

Jepp. Sowas in der Art.
Musst du nur noch für deine Tabelle anpassen.
Also etwa so:
ausblenden Quelltext
1:
2:
3:
select count(*) as reccount from Mitarbeiter 
where MitarbeiterNr = <DeinNeuerWertFuerMitarbeiter>
and KenntnisNr = <DeinNeuerWertFuerKenntnis>
csigg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 706

WIn XP, Win NT, Win2000, Suse8.0
Delphi 5, Delphi 6
BeitragVerfasst: Mo 18.04.05 09:08 
jap, da bin ich grad dabei das ganze anzupassen
csigg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 706

WIn XP, Win NT, Win2000, Suse8.0
Delphi 5, Delphi 6
BeitragVerfasst: Mo 18.04.05 09:14 
Hier ist mal mein SQL Text, der macht mir noch etwas probleme.

ausblenden Delphi-Quelltext
1:
2:
3:
sqlstatement := 'select count(*) as reccount from MAKENNTNISSE' +
                     'WHERE MANR=''' + dbeMANr.Text +
                     ''' AND KENTNR=''' + selection[i] + '''';[code:1:7d5e213fe8][/code:1:7d5e213fe8]
KidPaddle
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 351

WinXP,Linux
D7 Prof, XE7
BeitragVerfasst: Mo 18.04.05 09:34 
Du kannst einen Primarindex auf die Felder MANR und KENTNR erstellen. Damit wird schon auf Datenbankebene verhindert, das ein Satz doppelt angelegt werden kann.

Gruß
KidPaddle
csigg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 706

WIn XP, Win NT, Win2000, Suse8.0
Delphi 5, Delphi 6
BeitragVerfasst: Mo 18.04.05 09:45 
aber mit dem Primär Index darf ich doch auch keine Einträge öfters haben, oder??
Die Einträge sind ja oft gleich nur die kombination darf nur einmal auftreten, vorallem will ich das ganz ja davor abfangen und ne Meldung ausgeben.
KidPaddle
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 351

WinXP,Linux
D7 Prof, XE7
BeitragVerfasst: Mo 18.04.05 09:55 
Wenn Du den Primärindex auf beide(!) Felder erstellst, dann gilt die Eindeutigkeit nur für die Kombination. Die einzelnen Werte dürfen ruhig mehrmals vorkommen, nur die Kombination halt nicht. Wichtig dabei ist das Du bei der Erstellung beide Felder angibst und nicht nur eines.

Index kannst Du z. B. so erstellen:
ausblenden Quelltext
1:
CREATE UNIQUE INDEX PKMAKENNTNISSE ON MAKENNTNISSE(MANR, KENTNR);					


Ist zwar kein Primärindex, aber ein Unique - Index, was die gleiche Wirkung hat. Den Primarindex kannst Du glaube ich nur bei der Tabellenerstellung angeben, bin aber kein Interbase Spezialist.

Gruß
KidPaddle
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: Mo 18.04.05 10:20 
Hatte ich das nicht als dritte Möglichkeit vorgeschlagen? :gruebel:
KidPaddle
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 351

WinXP,Linux
D7 Prof, XE7
BeitragVerfasst: Mo 18.04.05 10:32 
Ja, aber anscheinend konnte er damit nichts anfangen und daher die erweiterte Version.

Gruß
KidPaddle
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Mo 18.04.05 10:44 
Mir gehen die Vorschläge nicht weit genug. :mrgreen: Ich erschlage solche Sachen folgendermaßen :

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
CREATE PROCEDURE RECSP (
    ID_ART INTEGER,
    MENGE INTEGER)
AS
DECLARE VARIABLE AENDERN INTEGER;
begin
  AENDERN = -1;
  select ID from REC where (ID_ART= :ID_ART) into :AENDERN;
  if (AENDERN < 0) then begin
    insert into REC (ID_ART,MENGE)
           values (:ID_ART,:ID_MENGE);
  end
  else begin
    update REC set MENGE=MENGE+ :MENGE
    where (ID_ART= :ID_ART);
  end
  suspend;
end


Hierbei wird die Arbeit an die DB delegiert, zu entscheiden, ob ein Update oder ein Insert nötig ist. Sollte es nur Inserts geben sollen und kein Update :shock: ,dann muß man ja nur den Update Zweig weglassen.

_________________
Gruß
Hansa
csigg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 706

WIn XP, Win NT, Win2000, Suse8.0
Delphi 5, Delphi 6
BeitragVerfasst: Mo 18.04.05 10:48 
also so sieht meine Lösung jetzt aus, falls es jemand interessiert:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
 sqlstatement := 'select count(*) as reccount from MAKenntnisse ' +
                    'where MANr='+ dbeMAnr.Text +
                    ' and KENTNr=' + selection[i] + '';

    with dmMesse.qrySpezialisten do
    begin
      Close;
      SQL.Clear;
      SQL.Add(sqlstatement);
      open;
      end;
      
    if dmMesse.qrySpezialisten.FieldByName('reccount').asinteger=0 then
    begin
    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: Mo 18.04.05 10:57 
@hansa:
Eine SP hatte ich als Empfehlung gegeben. Deine Lösung ist aber schon recht geschickt. :wink:

@csigg:
Sollte theoretisch funktionieren.
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Mo 18.04.05 13:03 
Der Witz ist vor allem das hier :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
begin
  with EingDM.RecSP do begin
    ParamByName ('ID_ART').AsInteger := ID_ART;
    ParamByName ('MENGE').AsInteger := m;
...
    ExecProc;
  end;
end;


Mit diesem sagenhaften Code ist alles erledigt. 8)

_________________
Gruß
Hansa