Board index » delphi » Changing StoredProcName (TStoredProc) at runtime

Changing StoredProcName (TStoredProc) at runtime

Hi,

Im having difficulties changing StoredProcName at runtime. My client
application makes a choice of stored procedure and returns different
resultsets. The problem is that when I do;
        StoredProc1.StoredProcName:='SomeProcName';
i can no longer find the Params! I would have the above line followed
by something like
        StoredProc1.ParamByName('SomeParamName').ParamType:=ptInput
but that gives med an exception.

Am i doing something wrong here? As a temporary solution i call
StoredProc1.Prepare - handle the exception, and is then able to adress
the Params...

Cheers,
Ole sorensen

 

Re:Changing StoredProcName (TStoredProc) at runtime


Ole,

   Try this:

Function RunStoredProc(MyDatabase :String;
                                    MyStoredProc :String;
                                    InputParam: String;
                                   OutputParam:Sting;
                                   InputVar:String ):String;
{This simple example runs a stored proc that accepts a single }
{input string, and returns a single value. }
{MyDatabase: The name of the database/BDE alias the other db components }
{    are connected to. }
{MyStoredProc: Name of stored procedure. }
{InputParam: Name of the input parameter }
{OutputParam: Name of the output parameter }
{InputVar: Value you want to pass to the stored procedure}

var
   StoredProc :TStoredProc;
begin
   StoredProc := TStoredProc.Create(nil);
   try
      begin                            { Begin try...finally block }
         StoredProc.DatabaseName := MyDatabase;
         StoredProc.StoredProcName := MyStoredProc;
         StoredProc.Params.CreateParam(ftString,InputParam,ptInput);
         StoredProc.Params.CreateParam(ftString,OutputParam,ptOutput);
         try
            begin                      { Begin try...except block }
               StoredProc.ParamByName(InputParam).Value := InputVar;
               StoredProc.ExecProc;
               Result := StoredProc.ParamByName(OutputParam).AsString;
            end;
         except
            on E:EDBEngineError do
               begin
                  ShowMessage(E.message);
                  Result := '';
               end;
         end;                          { End try...except block }
      end;                             { End try...finally block }
   finally
      StoredProc.Free;          {Free object}
   end;
end;

Good luck,
krf

ps.  I just came up with this, so reply back if I missed something.

Ole Chr. R. Sorensen <o...@datacom.dk400.dk> wrote in article
<354d93dd.11853...@forums.borland.com>...

Quote
> Hi,

> Im having difficulties changing StoredProcName at runtime. My client
> application makes a choice of stored procedure and returns different
> resultsets. The problem is that when I do;
>    StoredProc1.StoredProcName:='SomeProcName';
> i can no longer find the Params! I would have the above line followed
> by something like
>    StoredProc1.ParamByName('SomeParamName').ParamType:=ptInput
> but that gives med an exception.

> Am i doing something wrong here? As a temporary solution i call
> StoredProc1.Prepare - handle the exception, and is then able to adress
> the Params...

> Cheers,
> Ole sorensen

Re:Changing StoredProcName (TStoredProc) at runtime


If you change the properties of the TStoredProc, like the name or the
database, you'll have to reenter the parameters. Just save them before
changing the name of the storedproc you want to use and then restore them
before opening the storedproc.

Juan

Ole Chr. R. Sorensen wrote in message
<354d93dd.11853...@forums.borland.com>...

Quote
>Hi,

>Im having difficulties changing StoredProcName at runtime. My client
>application makes a choice of stored procedure and returns different
>resultsets. The problem is that when I do;
> StoredProc1.StoredProcName:='SomeProcName';
>i can no longer find the Params! I would have the above line followed
>by something like
> StoredProc1.ParamByName('SomeParamName').ParamType:=ptInput
>but that gives med an exception.

>Am i doing something wrong here? As a temporary solution i call
>StoredProc1.Prepare - handle the exception, and is then able to adress
>the Params...

>Cheers,
>Ole sorensen

Re:Changing StoredProcName (TStoredProc) at runtime


I tried using the TParams.CreateParam and it works fine. But I was
looking for a method to get the param names and datatypes from the
server. Like when you select a StoredProc at designtime. There would
be some validation value in doing that... Its getting that information
from the server, after StoredProcName has changed, that's the main
problem.

But thanks anyway.

BTW: You don't have to begin...end inside try...except blocks.

On 4 May 1998 12:14:25 GMT, "Kevin Frevert" <kfrev...@midwayarms.com>
wrote:

Quote
>Ole,

>   Try this:

<snipped code block>
Quote

>Good luck,
>krf

Re:Changing StoredProcName (TStoredProc) at runtime


What I would really like to do, is to get the param information from
the server. How do I do that?

It would not work to save the params before changing StoredProcName,
because the params changes with the choice of stored procedure from
the server.

On Mon, 4 May 1998 20:02:44 -0300, "Juan Jimenez (TeamB)"

Quote
<jjime...@caribe.net> wrote:
>If you change the properties of the TStoredProc, like the name or the
>database, you'll have to reenter the parameters. Just save them before
>changing the name of the storedproc you want to use and then restore them
>before opening the storedproc.

>Juan

>Ole Chr. R. Sorensen wrote in message
><354d93dd.11853...@forums.borland.com>...
>>Hi,

>>Im having difficulties changing StoredProcName at runtime. My client
>>application makes a choice of stored procedure and returns different
>>resultsets.

Re:Changing StoredProcName (TStoredProc) at runtime


In article <35502c60.8920...@forums.borland.com>,
  o...@datacom.dk400.dk (Ole Chr. R. Sorensen) wrote:

Quote

> What I would really like to do, is to get the param information from
> the server. How do I do that?

The only way I've been able to get the param information from the server is
when I initially set up a TStoredProc with database and stored procedure
information, it fills in the params.

However, one problem I'm encountering is that for my stored procedure with
three parameters (two input, one output), it only reads in the parameters
until it gets to either an out or in/out parameter, then it stops.  i.e., if
the first param is out, it won't read any params in automatically.

If the third param is the out param, it'll read in the first two in params
just fine.  If I try to run with less than the three params (in the
particular storedproc I'm using), I get an Oracle error saying too few
columns (expected).  If I add the three parameters manually, then I get a
"Capability not supported" message...Any ideas?

TIA

Andy Kazmaier

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

Other Threads