我在DB2中创建了一个用户定义的表类型,如下所示:
create table fullname as (street varchar(100), addr varchar(100))
我想知道如何在WITH
子句中使用它来基本上将子表列添加到此udtt。 WITH
子句如下:
WITH result (one, fullname) as
(
select one, two, three from info
UNION ALL
select one, two three from other_info
)
我想将第二列和第三列组合在一起作为一个由fullname表类型表示的列。这是可能的 - 它是如何完成的?
编辑: 假设两个表定义如下
info( one varchar(50), two varchar(50), three varchar(50) )
other_info( one varchar(50), two varchar(50), three varchar(50) )
因此WITH
子句将生成一个表,该表是info和other_info表的并集,并且此生成的表将具有模式
result (one varchar(50), fullname )
其中fullname
是用户定义的表类型,它包含union中的两个列属性two
和three
,作为一列。
如果表info
包含:
( 'Man', 'Peter', 'Griffin')
并且表other_info
包含:
('Baby', 'Stewie', 'Griffin')
然后WITH
子句将导致表
('Baby', ('Stewie', 'Griffin') )
('Man', ('Peter', 'Griffin') )
答案 0 :(得分:0)
使用以下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.