Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi Stored Procedure: Rückgabewert (https://www.delphipraxis.net/159053-stored-procedure-rueckgabewert.html)

fkerber 12. Mär 2011 14:07

AW: Stored Procedure: Rückgabewert
 
Hi,

das Ganze sieht dann nun so aus (und funktioniert auch):

Delphi-Quellcode:
var
  proc: TIBCStoredProc;
  ta: TIBCTransaction;
begin
  ta := TIBCTransaction.Create(nil);
  ta.DefaultConnection := ibc_mainDB;
  proc := TIBCStoredProc.Create(nil);
  proc.Transaction := ta;
  proc.StoredProcName := 'CREATECUSTOMER';
  proc.Prepare;
  proc.ParamByName('name').Value := custName;
  proc.ParamByName('title').Value := title;
  proc.ParamByName('forename').Value := forename;
  proc.ParamByName('surname').Value := surname;
  proc.ParamByName('street').Value := street;
  proc.ParamByName('houseNo').Value := houseNo;
  proc.ParamByName('country').Value := Uppercase(country);
  proc.ParamByName('zipcode').Value := zipcode;
  proc.ParamByName('cityname').Value := cityname;
  proc.ExecProc;
  result := proc.ParamByName('contactid').AsInteger;
  proc.Close;
  ta.Commit;
  proc.Free;
  ta.Free;
end;
Ist das dann so ok?


LG, Frederic

tsteinmaurer 12. Mär 2011 18:50

AW: Stored Procedure: Rückgabewert
 
Sieht ganz gut aus. Ich würde noch das Close weg geben und ein sauberes Exception-Handling machen. In etwa so:

Delphi-Quellcode:
var
  proc: TIBCStoredProc;
  ta: TIBCTransaction;
begin
  ta := TIBCTransaction.Create(nil);
  ta.DefaultConnection := ibc_mainDB;
  proc := TIBCStoredProc.Create(nil);
  proc.Transaction := ta;
  try
    ta.StartTransaction;
    try
      proc.StoredProcName := 'CREATECUSTOMER';
      proc.Prepare;
      proc.ParamByName('name').Value := custName;
      proc.ParamByName('title').Value := title;
      proc.ParamByName('forename').Value := forename;
      proc.ParamByName('surname').Value := surname;
      proc.ParamByName('street').Value := street;
      proc.ParamByName('houseNo').Value := houseNo;
      proc.ParamByName('country').Value := Uppercase(country);
      proc.ParamByName('zipcode').Value := zipcode;
      proc.ParamByName('cityname').Value := cityname;
      proc.ExecProc;
      result := proc.ParamByName('contactid').AsInteger;
      ta.Commit;
    except
      ta.Rollback;
      raise;
    end;
  finally
    proc.Free;
    ta.Free;
  end;
end;
Thomas

fkerber 13. Mär 2011 13:05

AW: Stored Procedure: Rückgabewert
 
Dankeschön!

omata 13. Mär 2011 13:26

AW: Stored Procedure: Rückgabewert
 
kleine Korrektur...
Delphi-Quellcode:
var
  proc: TIBCStoredProc;
  ta: TIBCTransaction;
begin
  ta := TIBCTransaction.Create(nil);
  proc := TIBCStoredProc.Create(nil);
  try
    proc.Transaction := ta;
    ta.DefaultConnection := ibc_mainDB;
    ta.StartTransaction;
    try
      proc.StoredProcName := 'CREATECUSTOMER';
      proc.Prepare;
      proc.ParamByName('name').Value := custName;
      proc.ParamByName('title').Value := title;
      proc.ParamByName('forename').Value := forename;
      proc.ParamByName('surname').Value := surname;
      proc.ParamByName('street').Value := street;
      proc.ParamByName('houseNo').Value := houseNo;
      proc.ParamByName('country').Value := Uppercase(country);
      proc.ParamByName('zipcode').Value := zipcode;
      proc.ParamByName('cityname').Value := cityname;
      proc.ExecProc;
      result := proc.ParamByName('contactid').AsInteger;
      ta.Commit;
    except
      ta.Rollback;
      raise;
    end;
  finally
    proc.Free;
    ta.Free;
  end;
end;

fkerber 18. Mär 2011 12:56

AW: Stored Procedure: Rückgabewert
 
Hi,

ich krieg die Krise.
Ich hatte den Code von omata übernommen und das hatte auch alles geklappt.
Seit eben kommt aber bei allem was ich tue (auch bei einer anderen Methode) nur noch folgende Fehlermeldung:

Zitat:

invalid transaction handle (expecting explicit transaction start)

Der Code im Moment:
Delphi-Quellcode:
var
  proc: TIBCStoredProc;
  ta: TIBCTransaction;
  query: TIBCQuery;
  i, contactid: Integer;
begin
  ta := TIBCTransaction.Create(nil);
  proc := TIBCStoredProc.Create(nil);
  query := TIBCQuery.Create(nil);
  result := True;
  try
    ta.DefaultConnection := ibc_mainDB;
    proc.Transaction := ta;
    query.Transaction := ta;
    ta.StartTransaction;
    try
      proc.StoredProcName := 'CREATECUSTOMER';
      proc.Prepare;
      proc.ParamByName('name').Value := custName;
      proc.ParamByName('title').Value := title;
      proc.ParamByName('forename').Value := forename;
      proc.ParamByName('surname').Value := surname;
      proc.ParamByName('street').Value := street;
      proc.ParamByName('houseNo').Value := houseNo;
      proc.ParamByName('country').Value := Uppercase(country);
      proc.ParamByName('zipcode').Value := zipcode;
      proc.ParamByName('cityname').Value := cityname;
      proc.ExecProc;
      contactid := proc.ParamByName('contactid').AsInteger;

      query.SQL.Text :=
        'INSERT INTO DIALNUMBER (NUMBERTYPE, NUMBER, CONTACTID) VALUES (:type, :no, :contactid)';
      query.Prepare;
      for i := 0 to length(dialNo) - 1 do
      begin
        query.ParamByName('type').Value := dialNo[i][0];
        query.ParamByName('no').Value := dialNo[i][1];
        query.ParamByName('contactid').Value := contactid;
        query.Execute;
      end;

      ta.Commit;
    except
      result := False;
      ta.Rollback;
      raise;
    end;
  finally
    query.Free;
    proc.Free;
    ta.Free;
  end;
end;

Was mache ich falsch?
Es tritt auch bei den Codes von Thomas und omata auf.
Ich muss also irgendwo was generelles verdreht haben, aber ich weiß nicht was?!
Neustart half auch nicht.

LG, Frederic


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:34 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz