DB2 saves results from final table

The FINAL TABLE clause is great for returning values ​​from DML to DB2, for example:

SELECT id
FROM FINAL TABLE
( 
  INSERT INTO mySchema.myTable (val)
  VALUES ('data')
)            

However, there seems to be no way to store the results of this query in another table, storing the contents somewhere. For example, both of the following errors with the error "Link to the data change table is not allowed where indicated." (I am running DB2 for I v7.1):

CREATE TABLE mySchema.otherTable AS (
SELECT id
FROM FINAL TABLE
( 
  INSERT INTO mySchema.myTable (val)
  VALUES ('data')
)
) WITH DATA      

After creating mySchema.otherTable in a separate CREATE TABLE statement, this also fails:

INSERT INTO mySchema.otherTable (ID)
SELECT id
FROM FINAL TABLE
( 
  INSERT INTO mySchema.myTable (val)
  VALUES ('data')
)
+1
source share
2 answers

Not sure if this works in the i Series, but DB2 for LUW allows you to do this:

with i1 (id) as (
  SELECT id
  FROM FINAL TABLE
  ( 
    INSERT INTO mySchema.myTable (val)
    VALUES ('data')
  )
)
select * from new table (
  INSERT INTO mySchema.otherTable (ID) 
  select id from i1
)
+2
source

FINAL TABLE IBM OS V7R1, , DB2 for LUW . , .

SQL SET, 2 . SQL- , ​​ , DB2 for LUW. temp.

create variable MY_SCHEMA.MY_TABLE_ID
;
set MY_SCHEMA.MY_TABLE_ID = 
    ( select ID 
      from final table ( insert into MY_SCHEMA.MY_TABLE values ('data') ) ) 
;
insert into MY_SCHEMA.MY_OTHER_TABLE ( ID, DATA ) 
values( MY_SCHEMA.MY_TABLE_ID, 'more data' )
;

SQL V7R1: . , , , .

SQL SELECT INTO .

+1

Source: https://habr.com/ru/post/1671433/


All Articles