0

我是 plpgsql 的新手,我创建了一个存储过程来将表数据导入 refcursor,并且我正在尝试从 plpgsql 和 c# 调用该过程以检查过程/c# 代码是否工作正常。

程序:

CREATE OR REPLACE FUNCTION public.proc_get_test(out tbldata refcursor)
returns refcursor
LANGUAGE plpgsql
AS $function$
BEGIN
    open tbldata for
    select * from arc_mmstbrndgroup;
END;
$function$;

从 plpgsql 调用过程:

SELECT proc_get_test('cur');

输出:
调用过程

然后尝试从 refcursor 获取数据:

FETCH ALL FROM cur;

SQL 错误 [34000]: 错误: 游标“cur”不存在

尝试的另一种方法:

BEGIN;
SELECT proc_get_test('cur');
FETCH ALL FROM cur;
COMMIT;

输出:

输出

C#:

var p = new PostgreSQLDynamicParameters();
p.Add("tbldata", dbType: NpgsqlDbType.Refcursor, direction: ParameterDirection.Output);

using (var multi = _connection.QueryMultiple("proc_get_test", param: p, commandType: CommandType.StoredProcedure))
{
    List<PostgresModel> dataMaster = multi.Read<PostgresModel>().AsList();

    return new ResponseModel { ResultSet = dataMaster, StatusCode = 1, StatusDescription = "Success" };
}

Api 仅接收 1 行也具有空值

谁能帮我在 plpgsql 和 c# 中调用该过程?

4

1 回答 1

0

所以我尝试在 plpgsql 中创建一个具有多个引用的演示函数,如下所示:

CREATE OR REPLACE FUNCTION public.proc_get_multiple_cursors_test()
returns setof refcursor
LANGUAGE plpgsql
AS $$
DECLARE
    ref1 refcursor:= 'ref1';
    ref2 refcursor:= 'ref2';
BEGIN
    open ref1 for 
    select * from arc_mmstbrndgroup;
    return next ref1;

    open ref2 for 
    select * from arc_mmstbrndgroup;
    return next ref2;
END;
$$

并像这样从 c# 调用这个函数:

_connection.Open();
NpgsqlTransaction trans = _connection.BeginTransaction();
using (var multi = _connection.QueryMultiple("proc_get_multiple_cursors_test", null, transaction: trans, commandType: CommandType.StoredProcedure))
{
    var result = multi.Read();
    var ref1 = _connection.QueryMultiple("FETCH ALL IN ref1", null, commandType: CommandType.Text, transaction: trans);
    List<PostgresModel> dataMaster = ref1.Read<PostgresModel>().AsList();
    var ref2 = _connection.QueryMultiple("FETCH ALL IN ref2", null, commandType: CommandType.Text, transaction: trans);
    List<PostgresModel> dataMaster2 = ref2.Read<PostgresModel>().AsList();
    trans.Commit();
    _connection.Close();

    return new ResponseModel
    {
        ResultSet = new
        {
            master1 = dataMaster,
            master2 = dataMaster2
        },
        StatusCode = 1,
        StatusDescription = "Success"
    };
}

现在它返回两个游标的输出

于 2019-11-19T09:36:11.337 回答