Avatar of Stef Merlijn
Stef Merlijn
Flag for Netherlands asked on

TFDStoredProc param not found

Hi,

In my Delphi application I have a TFDStoredProc (name: aspCopyMyRecord) to access a MS SQL Server database.
Sometimes when the procedure below is executed, I get an error:
aspCopyMyRecord: Parameter '@ID' not found

procedure TDM3.CopyMyRecord(vID, vID2 : Integer; CONST vCode : String);
begin
  try
    with aspCopyMyRecord do
    begin
      ResourceOptions.UnifyParam := False;
      Close;
      StoredProcName := 'dba_CopyMyRecord';
      FetchOptions.Items := aspCopyMyRecord.FetchOptions.Items - [fiMeta];
      Command.FillParams(aspCopyMyRecord.Params);
      Prepare;
      Params.ParamByName('@ID').Value     := vID;
      Params.ParamByName('@ID2').Value  := vID2;
      Params.ParamByName('@Code').Value    := vCode;
      ExecProc;
    end;
  finally
    CheckReturn(aspCopyMyRecord);
  end;
end;

Open in new window


The parameters in SQL Server Stored Proc are named like:
CREATE PROCEDURE dba_CopyMyRecord( 
     @ID INT 
   , @ID2 INT
   , @Code nVarChar(50)
   , @ReturnValue INT OUTPUT)

Open in new window


Can anybode see what might be wrong with my code?
Thanks.
Delphi

Avatar of undefined
Last Comment
Stef Merlijn

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Geert G

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Stef Merlijn

ASKER
For as far as I investgated it, FireDac has a mechanism that removes the @ for some databases (o.a. SQL Server). That's why I use:
  ResourceOptions.UnifyParams := False;

Open in new window

But for some reason this doesn't always work.
Depending on the DBMS, the parameter name may be prefixed with '@' or ResourceOptions.UnifyParams set to True.
Info on: ResourceOptions.UnifyParams

In my development enviroment it works fine as it is setup now, but some customers still generate the error now and than.
Bruno Buesser

How did you create the stored procedure in your program?
If I create the stored procedure in code (with MySQL) I do the following:

  FScCreateUserProc:= TFDStoredProc.Create(Self);
  with FScCreateUserProc do
  begin
    Connection:= FConnection;
    StoredProcName:= 'ScCreateUser';
    Params.Clear;
    Params.Add('AUserName', ftString,45,ptInput);
    Params.Add('AFirstName', ftString,45,ptInput);
    Params.Add('ALastName', ftString,45,ptInput);
    Params.Add('ARoleName', ftString,45,ptInput);
    Params.Add('AId', ftString,4,ptOutput);
  end;

Open in new window

After that you can set the values and execute the procedure:

     
with FScCreateUserProc do
      begin
        Params.ParamByName('AUserName').AsString:= LoginName;
        Params.ParamByName('AFirstName').AsString:= FirstName;
        Params.ParamByName('ALastName').AsString:= LastName;
        Params.ParamByName('ARoleName').AsString:= RoleName;
        ExecProc;
        Id:= Params.ParamByName('AId').AsInteger;

Open in new window

Stef Merlijn

ASKER
Thanks for the help.
Both solution should probably work fine, but I've decided to use TADOStroredProc in stead of TFDStoredProc, as I kept getting errors from customers.
Your help has saved me hundreds of hours of internet surfing.
fblack61