如何在 Firebird 2.5 中选择程序
我正在使用 Firebird Embedded v2.5。如何在查询(SELECT)中使用过程?
我的程序:
SET TERM ^ ;
CREATE PROCEDURE FN_TEST( Y INTEGER )
RETURNS( X INTEGER)
AS
BEGIN
X = Y + 1;
END^
SET TERM ; ^
我想列出由某个程序修改的表的某些字段,如下所示:
SELECT some_table_field_1,
fn_test( 4 ) AS zzz,
some_table_field_2,
fn_test( some_table_field_2 ) AS field_2_modified
FROM tb_test
需要结果(表):
some_table_field_1 zzz some_table_field_2 field_2_modified
---------------------------------------------------------------------------
aaa 5 14 15
bbb 5 23 24
www 5 75 76
这个东西在 PostgreSQL 中工作正常,但我不知道如何在 Firebird 中执行此操作。
I'm using Firebird Embedded v2.5. How to use procedures in query (SELECT) ?
My procedure:
SET TERM ^ ;
CREATE PROCEDURE FN_TEST( Y INTEGER )
RETURNS( X INTEGER)
AS
BEGIN
X = Y + 1;
END^
SET TERM ; ^
I want to list some field of table modified by some procedure, like this:
SELECT some_table_field_1,
fn_test( 4 ) AS zzz,
some_table_field_2,
fn_test( some_table_field_2 ) AS field_2_modified
FROM tb_test
Need results (table):
some_table_field_1 zzz some_table_field_2 field_2_modified
---------------------------------------------------------------------------
aaa 5 14 15
bbb 5 23 24
www 5 75 76
This thing works fine in PostgreSQL, but I don't know how to do this in Firebird.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
FN_TEST
是一个可执行过程:它可以通过EXECUTE PROCEDURE
语句调用,并返回一组输出参数。在 Firebird 2.x 中,只有可选择的存储过程可以“用作”视图/表(请参阅Firebird 存储过程)。
因此:
产生一个 invalid request BLR at offset... 错误。
您可以按照建议更改您的程序,但实际上,您需要的功能已在 Firebird 3 中以<的形式引入a href="https://www.firebirdsql.org/file/documentation/release_notes/html/en/3_0/rlsnotes30.html#rnfb30-psql-funcs" rel="nofollow noreferrer">存储函数:
更多详细信息,请参见 Firebird 3 中的 PSQL 函数。
FN_TEST
is an executable procedure: it can be called via theEXECUTE PROCEDURE
statement and it returns a single set of output parameters.In Firebird 2.x only a selectable stored procedure can be "used" as a view / table (see Firebird Stored Procedures).
So:
produces an invalid request BLR at offset... error.
You could change your procedure as suggested but, actually, the feature you need has been introduced in Firebird 3 in the form of stored function:
Further details in Functions with PSQL in Firebird 3.
尝试
Try
使用 UDF 来管理字段的计算。
存储过程仅在 FROM 子句中被允许。
Use UDF in order to manage calculation on fields.
Stored procedure are admited only in the FROM Clause.
试试这个
Try this
正如 JustMe 所说,您无法在选择中调用存储过程。您只能在 FROM 部分中调用存储过程。解决您的问题的另一个解决方案是创建一个如下所示的可选过程:
运行该代码后,您可以简单地查询
select * from myproc(4)
并获得您想要的内容。As JustMe said, you can't call stored procedures in a select. You can call stored procedure only in the FROM section. Another solution for your problem is to create a selectable procedure like this:
After run that code, you can simply query
select * from myproc(4)
and get what you want.您无法调用 Firebird 选择列表中的存储过程。您必须编写具有所需结果的可选过程,或者编写一个 UDF 函数来执行
fn_test
过程中的操作。对于您的情况,最简单的方法是:
You can't call stored procedures in Firebird's select list. You have to write the selectable procedure with desire result or write an UDF function to do what you have in
fn_test
procedure.For your case the simplest way is:
您可以使用 EXECUTE BLOCK 请查看 EXECUTE BLOCK
You can use EXECUTE BLOCK Please have a look EXECUTE BLOCK