Home InterBase Firebird Community About us Search Help English Russian
Everything about Borland InterBase and Firebird
Get Firebird power with FIBPlus
 
Anonymous Login / Register
Site News
Third-party Product News
Programming
Miscellaneous
Encyclopedia
FAQ


Search the site


Subscribe the site news

Sections:
News
E-mail:


InterBase World is a Full Member of Firebird Foundation



Login


Hits 2622120
2250
Hosts 152082
718
Visitors 771387
997

5



Powered by Bitrix Site Manager - Content Management & Portal Solutions

Home / Article archives / Programming

Executing TIBStoredProc with one line of code


01/27/2003  | Views: 5135

TIBStoredProc is handy, but multiple lines of code are required to execute it. The routine in this article handles preparing, assigning params, execution and transactions for you.

Author: Erwin Molendijk

{ 
  ExecSP 
  Execute a InterBase Stored Procedure.  Transaction gets Committed after excution. 

  input: 
    SP = InterBase Stored Procedure 
    P  = Array with parameters for the SP. No param checking! 

  output: 
    Check the SP.Params for output (if any). 
} 
procedure TSPMod.ExecSP(SP: TIBStoredProc; P: array of Variant); 
var 
  A,B: Integer; 
begin 
  // make sure there's a transaction context 
  if not SP.Transaction.Active then 
    SP.Transaction.StartTransaction; 

  try 
    // make sure stored procedure is closed 
    SP.Close; 

    // prepare (attach params) 
    if not SP.Prepared then 
      SP.Prepare; 

    // Set all Input params 
    B := 0; 
    for A := 0 to SP.ParamCount-1 do 
      if (SP.Params[A].ParamType in [ptInput, ptInputOutput]) then 
      begin 
        SP.Params[A].Value := P[B]; 
        Inc(B); 
      end; 

    // run the procedure on the server 
    SP.ExecProc; 
  finally 
    // commit 
    SP.Transaction.Commit; 
  end; 
end; 


Examples: 

Assume you have a datamodule called SPMod. And assume it contains some stored procedures: 
  SPMod.spOpenenSession 
  SPMod.spGetTicketNr 

The following routines can be added to encapsulate the StoredProcs. 
   
// Example without returning data: 

procedure TSPMod.OpenSession(SessionID: Integer); 
begin 
  ExecSP(spOpenSession, [SessionID]); 
end; 


// Example with a integer as result 

function TSPMod.GetTicketNr: Integer; 
begin 
  ExecSP(spGetTicketNr, [CurrentSessionID]); 
  Result := spGetTicketNr.ParamByName('TicketNr').AsInteger; 
end; 

Back to the news section


 Last articles 
09/24/2007 IBSurgeon would like to inform you about a series of free Firebird technical seminars

09/23/2007 Firebird System Tables (Parts II+III)

09/20/2007 Firebird Maestro 7.9 released

09/19/2007 And the Winner is —

09/19/2007 Unable to access fireruby lib - with firebird 2.0.x and ror

news archive