如何在 Firebird 中执行返回结果集的过程

How to execute procedure returning resultset in Firebird

我有以下table

create table LIST_PIPE_TABLE
(
  ID INT,
  ITEM          VARCHAR(4000),
  IS_FOLDER     VARCHAR(10)
)

有 3 行数据

insert into LIST_PIPE_TABLE values(1,'Victorias Secret','true')
insert into LIST_PIPE_TABLE values(2,'Porsche','true')
insert into LIST_PIPE_TABLE values(3,'Babbolat','false')

一个存储过程应该 return 结果集

CREATE or alter PROCEDURE LIST_PIPE
RETURNS 
(   col1 varchar(4000),
    col2 varchar(10)
)
  AS  
begin
    FOR SELECT ITEM AS ITEM
       ,IS_FOLDER AS IS_FOLDER
      FROM LIST_PIPE_TABLE 
    into :col1, :col2
    do begin       
            suspend;          
     end 
end

当我尝试用下面的语句执行它时

execute procedure LIST_PIPE

唯一的顶行是 returned

COL1              COL2
Victorias Secret  true

有什么问题请指教。我应该如何执行它以查看它设计的所有 3 行 return?

当你在存储过程中有 suspend 时,它被称为 "selectable stored sprocedure",正如名字所说你 select 来自它,所以:

select * from LIST_PIPE

作为,你需要用SELECT * FROM <your procedure>来做一个select可行的程序(即:它包含一个SUSPEND)。

Interbase 6 Embedded SQL Guide(参见 InterBase 6.0 手册)说:

There are two types of procedures that can be called from an application:

  • Select procedures that an application can use in place of a table or view in a SELECT statement. A select procedure must return one or more values, or an error results.
  • Executable procedures that an application can call directly, with the EXECUTE PROCEDURE statement. An executable procedure may or may not return values to the calling program.

Both kinds of procedures are defined with CREATE PROCEDURE and have the same syntax. The difference is in how the procedure is written and how it is intended to be used. Select procedures always return zero or more rows, so that to the calling program they appear as a table or view. Executable procedures are simply routines invoked by the calling program that can return only a single set of values.

In fact, a single procedure conceivably can be used as a select procedure or an executable procedure, but this is not recommended. In general a procedure is written specifically to be used in a SELECT statement (a select procedure) or to be used in an EXECUTE PROCEDURE statement (an executable procedure).

在协议级别,EXECUTE PROCEDURE 语句将始终产生单行结果(可能为空),而 SELECT * FROM <procedure> 的行为与 select 来自 table 或视图。这意味着如果使用 EXECUTE PROCEDURE 调用 selectable 过程,Firebird 本身将仅从存储过程中获取一行,然后结束过程的执行。

不幸的是,可以使用 EXECUTE PROCEDURE 和 select 可行的程序。 SUSPEND 上的 Interbase 6 语言参考明确提到 "SUSPEND 不应在 executable 过程中使用。""(措辞很奇怪,因为 SUSPEND 的存在使它 select 可用,尽管这里他们的意思是用 EXECUTE PROCEDURE 调用它是不可取的)。