LoginSignup
0
0

More than 5 years have passed since last update.

Firebirdで、テーブルのSelect結果をストアドプロシージャに渡しJoinして返す

Last updated at Posted at 2018-06-11

準備

サンプルテーブル

create table T (
  X1 integer not null,
  constraint T_PK primary key (X1)
);

サンプルデータ投入

とりあえず20件ほど

insert into T
with recursive V_SEQ as (
  select 1 as X1 from rdb$database
  union all
  select X1+1 from V_SEQ
  where
    X1 < 20
)
select * from V_SEQ;
commit;

テスト用ストアドプロシージャ定義

ひとつ前と次の整数値を返すだけのストアドプロシージャ

set term # ;
create procedure f(X1 integer)
returns (X_PRED integer, X_SUCC integer)
as
begin
  X_PRED = X1-1;
  X_SUCC = X1+1;
  suspend;
end#
set term ; #

方法解説

以下のように、そのまま直積取ってもエラーになります

select * from T
cross join f(T.X1)

Invalid request BLR at offset 32
Procedure F is not selectable (it does not contain a SUSPEND statement)
ステートメント: select * from T
cross join f(T.X1)

ではどうすればよいかといえば、外部結合を使います。

select * from T
left outer join f(T.X1) on 1=1

on 以降は等価結合になれば、なんでもいいです。

ちゃんととれました。

          X1       X_PRED       X_SUCC
============ ============ ============
           1            0            2
           2            1            3
           3            2            4
           4            3            5
           5            4            6
           6            5            7
           7            6            8
           8            7            9
           9            8           10
          10            9           11
          11           10           12
          12           11           13
          13           12           14
          14           13           15
          15           14           16
          16           15           17
          17           16           18
          18           17           19
          19           18           20
          20           19           21

Firebird 2.1系と2.5系で試しましたが、おそらく2.0系でもいけるとおもいます(希望的観測)

元ネタ

How to JOIN a table and selectable stored procedure?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0