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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With