Delphi:如何获取存储过程的输出参数的值? [英] Delphi: How to get the value of an output parameter of a stored procedure?

查看:258
本文介绍了Delphi:如何获取存储过程的输出参数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Delphi中以编程方式创建一个SQLDataSet,并使用它来执行一个存储过程并获取一个输出参数的值。看起来很容易,但我不能让它工作。

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.

这是一个在SQL Server中的哑存储过程:

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.

推荐答案

看起来像是设置CommandType和SchemaName,不创建Param:

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;

这篇关于Delphi:如何获取存储过程的输出参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆