使用以下 SQL 脚本,DB2 V10.5 - Linux:
connect to pocdb;
drop table stack.with_test_1;
drop table stack.with_test_2;
drop table stack.with_test_results;
create table stack.with_test_1 (
one varchar(50),
two varchar(50),
three varchar(50)
);
create table stack.with_test_2 (
one varchar(50),
two varchar(50),
three varchar(50)
);
create table stack.with_test_results (
one varchar(50),
fullname varchar(100)
);
insert into stack.with_test_1 values ('x','fn1','ln1');
insert into stack.with_test_1 values ('x','fn2','ln2');
insert into stack.with_test_1 values ('x','fn3','ln3');
insert into stack.with_test_2 values ('y','fn4','ln4');
insert into stack.with_test_2 values ('y','fn5','ln5');
insert into stack.with_test_2 values ('y','fn6','ln6');
insert into stack.with_test_results
with union_test as (
select one, two, three from stack.with_test_1
union all
select one, two, three from stack.with_test_2
)
select
one,
two || ' ' || three as full_name
from
union_test
;
select * from stack.with_test_results order by one;
connect reset;
terminate;
产生这些结果:
connect to pocdb
Database Connection Information
Database server = DB2/LINUXX8664 10.5.3
SQL authorization ID = DB2INST1
Local database alias = POCDB
drop table stack.with_test_1
DB20000I The SQL command completed successfully.
drop table stack.with_test_2
DB20000I The SQL command completed successfully.
drop table stack.with_test_results
DB20000I The SQL command completed successfully.
create table stack.with_test_1 ( one varchar(50), two varchar(50), three varchar(50) )
DB20000I The SQL command completed successfully.
create table stack.with_test_2 ( one varchar(50), two varchar(50), three varchar(50) )
DB20000I The SQL command completed successfully.
create table stack.with_test_results ( one varchar(50), fullname varchar(100) )
DB20000I The SQL command completed successfully.
insert into stack.with_test_1 values ('x','fn1','ln1')
DB20000I The SQL command completed successfully.
insert into stack.with_test_1 values ('x','fn2','ln2')
DB20000I The SQL command completed successfully.
insert into stack.with_test_1 values ('x','fn3','ln3')
DB20000I The SQL command completed successfully.
insert into stack.with_test_2 values ('y','fn4','ln4')
DB20000I The SQL command completed successfully.
insert into stack.with_test_2 values ('y','fn5','ln5')
DB20000I The SQL command completed successfully.
insert into stack.with_test_2 values ('y','fn6','ln6')
DB20000I The SQL command completed successfully.
insert into stack.with_test_results with union_test as ( select one, two, three from stack.with_test_1 union all select one, two, three from stack.with_test_2 ) select one, two || ' ' || three as full_name from union_test
DB20000I The SQL command completed successfully.
select * from stack.with_test_results order by one
ONE FULLNAME
-------------------------------------------------- -------------------------
---------------------------------------------------------------------------
x fn1 ln1
x fn2 ln2
x fn3 ln3
y fn4 ln4
y fn5 ln5
y fn6 ln6
6 record(s) selected.
connect reset
DB20000I The SQL command completed successfully.
terminate
DB20000I The TERMINATE command completed successfully.