Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better way of passing parameters to a TADOStoredProc (Delphi)

I am needing to convert a large amount of SQL queries into stored procedures. I have some code that updates about 20 or 30 values at one time in one Delphi procedure. I can handle creating a stored procedures to do such a thing. The problem is my way to pass parameters to stored procedures is very bulky like this:

    with stored_procedure do......
    Param := Parameters.AddParameter;
    Param.Name := '@SSN';
    Param.Value := edtSSN.text;

    Param := Parameters.AddParameter;
    Param.Name := '@FirstName';
    Param.Value := edtFirstName.Text;

    Param := Parameters.AddParameter;
    Param.Name := '@LastName';
    Param.Value := edtLastName.Text;

    Param := Parameters.AddParameter;
    Param.Name := '@UserRID';
    Param.Value:= GetRIDFromCombo(cbUser);

I also am not sure if that causes a memory leak(is it necessary to free such TParameter objects?)

Anyone have a better way of handling a large amount of parameters? (I can not use a new library. I must use ADO, and the SQL I use is MSSQL) (also, I'm NOT using ADO.net)

like image 464
Earlz Avatar asked Jul 21 '09 14:07

Earlz


People also ask

How do you pass a parameter to a procedure?

To pass one or more arguments to a procedure In the calling statement, follow the procedure name with parentheses. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.

How many types of parameters in stored procedure?

As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT.

What is parameterized procedure?

A parameter represents a value that the procedure expects you to supply when you call it. The procedure's declaration defines its parameters. You can define a procedure with no parameters, one parameter, or more than one. The part of the procedure definition that specifies the parameters is called the parameter list.

How do I run a stored procedure in Delphi?

To execute a stored procedure, drop a TFDStoredProc component on a form. TFDStoredProc. Connection will be automatically set to point to a TFDConnection on this form, if any. Then, optionally set the CatalogName, SchemaName, and PackageName properties or choose their values from a dropdown list.


1 Answers

There's an accepted answer :-), but I want to point you to simpler and easier way to define and use the parameters with one line :

stored_procedure.Parameters.CreateParameter('SSN',ftString,pdInput,30,edtSSN.text);

It's simple and flexible, because you can define the input and output parameters with same line.

and from Delphi help:

function CreateParameter(const Name: WideString; DataType: TDataType;
    Direction: TParameterDirection; Size: Integer; 
    Value: OleVariant): TParameter;
like image 148
Mohammed Nasman Avatar answered Oct 19 '22 12:10

Mohammed Nasman