在 Devart 实体模型中添加 postgresql 函数

发布于 2024-11-19 13:04:22 字数 740 浏览 8 评论 0原文

我正在尝试在 devart 实体模型中导入 postgresql 函数,我想在其中返回一个“自定义”表,但我无法让它工作

我像这样定义我的函数:

CREATE OR REPLACE FUNCTION "GetJournalEntriesByVoucherId"(bigint)
  RETURNS SETOF record AS
$BODY$
SELECT 
    JE."JournalEntryID"
    JE."Amount",
    JE."EntryText",
FROM
    "JournalEntries" AS JE
WHERE
    JE."FK_Voucher"=$1
$BODY$
  LANGUAGE sql VOLATILE STRICT
  COST 100
  ROWS 1000;
ALTER FUNCTION getjournalentriesbyvoucherid(bigint) OWNER TO sqluser;

并使用帖子 #2 中的步骤在 http://www.devart.com/forums/viewtopic.php?p=71252 但我收到错误消息

数据读取器与指定的 Model.Entity1 不兼容。 JournalEntryID 类型的成员在数据读取器中没有同名的对应列”

I'm trying to import a postgresql function in an devart entity model, where I want to return a "custom" table, but I just can't get it working

I define my function like this:

CREATE OR REPLACE FUNCTION "GetJournalEntriesByVoucherId"(bigint)
  RETURNS SETOF record AS
$BODY$
SELECT 
    JE."JournalEntryID"
    JE."Amount",
    JE."EntryText",
FROM
    "JournalEntries" AS JE
WHERE
    JE."FK_Voucher"=$1
$BODY$
  LANGUAGE sql VOLATILE STRICT
  COST 100
  ROWS 1000;
ALTER FUNCTION getjournalentriesbyvoucherid(bigint) OWNER TO sqluser;

And used the steps in post #2 in http://www.devart.com/forums/viewtopic.php?p=71252 but I get the error message

The data reader is incompatible with the specified Model.Entity1. A member of the type, JournalEntryID, does not have a corresponding column in the data reader with the same name"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-11-26 13:04:22

我经常比较幸运地使用 OUT 变量来返回设置的存储过程。我不知道这对你的情况是否有帮助,但你可以尝试一下。老实说,我不知道这是否改变了函数的工作方式或只是改变了它的定义方式......

CREATE OR REPLACE FUNCTION "GetJournalEntriesByVoucherId"(BIGINT,
    JournalEntryID OUT DATATYPE,
    Amount OUT INT,
    EntryText OUT VARCHAR,
)
RETURNS SETOF record AS
$BODY$
....

I've often had better luck using OUT variables for set-returning stored procedures. I don't know if this will help in your case, but you might try it. I honestly don't know if this changes the way the function works or just the way it's defined...

CREATE OR REPLACE FUNCTION "GetJournalEntriesByVoucherId"(BIGINT,
    JournalEntryID OUT DATATYPE,
    Amount OUT INT,
    EntryText OUT VARCHAR,
)
RETURNS SETOF record AS
$BODY$
....
勿忘初心 2024-11-26 13:04:22

@Flimzy 是正确的。
当函数返回 setof 记录时,从函数返回的读取器包含“(field1.Value, ..., fieldk.Value)”形式的记录,并被视为 varchar。
当指定 out 参数时,这些参数的名称和类型让服务器知道读取器中的字段是什么,并正确返回值。我们已经在最新的 5.30.180 版本中修复了这种情况下的一些错误,现在可以使用了。
使用强类型 setof (CompositeType) 的解决方案也是一种合适的解决方案。

@Flimzy is correct.
When a function returns setof record the reader returned from the function contains the record in form '(field1.Value, ..., fieldk.Value)' and is treated as varchar.
When out parameters are specified, the names and types of these parameters let the server know what the fields are in the reader, and the values are returned correctly. We have fixed some errors in this scenario in the latest 5.30.180 build, it works now.
The solution with strongly-typed setof (CompositeType) is an appropriate one as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文