0
CREATE OR REPLACE FUNCTION some_function(_limit integer, _skip integer, _sortcolumn text, _sortasc boolean)
  RETURNS SETOF some_table AS
$BODY$

begin

 return query execute 'select * from some_table order by "'||_sortcolumn||'"' ||case when _sortasc then 'asc' else 'desc' end ||' limit $1 offset $2;' using _limit, _skip;

end;
$BODY$
  LANGUAGE plpgsql STABLE SECURITY DEFINER
  COST 100
  ROWS 100;

我想通过引用传递参数,所以我可以将总行数分配给参数。到目前为止,我认为这是不可能的。有什么建议吗?我正在使用 C#

编辑

我发现“RAISE NOTICE”可能有用。仍然找到一种通过 npgsql 在 .Net 中接收通知的方法

4

2 回答 2

2

有几种方法可以做到这一点。如果您使用命令对象直接调用此函数,则 ExecuteScalar 函数将返回命令返回的行数。

rowCountValue = (int)command.ExecuteScalar();

或者,您可以在函数定义中执行此操作;

CREATE OR REPLACE FUNCTION some_function(_limit integer, _skip integer, _sortcolumn text, _sortasc boolean, OUT _row_count integer)
  RETURNS SETOF some_table AS
$BODY$
DECLARE
    _result RECORD;
BEGIN
EXECUTE 'SELECT * FROM some_table ORDER BY "'||_sortcolumn||'"' ||case when _sortasc then 'asc' else 'desc' end ||' limit $1 offset $2;' INTO _result using _limit, _skip;

GET DIAGNOSTICS _row_count = ROW_COUNT;

return result;

END;
$BODY$
  LANGUAGE plpgsql STABLE SECURITY DEFINER
  COST 100
  ROWS 100;

如果您查询建筑没问题,那么这个应该工作得很好。

希望这可以帮助。

于 2010-12-27T15:53:44.040 回答
1

You may also give a try to change your function to return set of refcursors instead and return a resultset with you rows count and another set with your data.

Check out "Getting full results in a DataSet object: Using refcursors" section from Npgsql Users Manual: http://manual.npgsql.org There you'll find how to do that.

I hope it helps.

于 2011-01-02T15:17:04.873 回答