Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: How to get the value of an output parameter of a stored procedure?

I want to programatically create a SQLDataSet in Delphi and use it to execute a Stored Procedure and get the value of an output parameter. Looks easy but I can't make it work.

Here is a dumb stored procedure in SQL Server:

CREATE PROCEDURE [dbo].getValue  @x INT OUTPUT
AS
BEGIN
  SET @x = 10;
END

Now here is one of the variations that I tried and didn't work:

proc := TSQLDataSet.Create(nil);
proc.SQLConnection := DefaultConnection;
proc.CommandText := 'getValue';
proc.Params.CreateParam(ftInteger, '@x', ptOutput);
proc.Params.ParamByName('@x').Value := 0;
proc.ExecSQL(False);
value := newIdProc.Params.ParamByName('@x').AsInteger;

I thought it would be easy, but there are some registred bugs around this issue.

like image 223
neves Avatar asked Aug 06 '09 23:08

neves


1 Answers

Looks like it works if you set the CommandType and SchemaName and don't create the Param:


proc := TSQLDataSet.Create(nil);
proc.SQLConnection := DefaultConnection;

proc.CommandType := ctStoredProc;
proc.SchemaName  := 'dbo';
proc.CommandText := 'getValue';

proc.ExecSQL(False);

value := proc.Params.ParamByName('@x').AsInteger;

like image 51
jasonpenny Avatar answered Oct 26 '22 08:10

jasonpenny