【问题标题】:How to use user defined table type in DB2 with clause?如何在 DB2 with 子句中使用用户定义的表类型?
【发布时间】:2016-03-24 01:50:45
【问题描述】:

我在 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
)

我想将第二列和第三列组合为一列,由全名表类型表示。这可能吗 - 它是如何完成的???

编辑: 假设这两个表定义如下

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 是用户定义的表类型,它包含来自联合的两个列属性twothree,作为一列。

所以如果表 info 包含:

( 'Man', 'Peter', 'Griffin')

other_info包含:

('Baby', 'Stewie', 'Griffin')

那么WITH 子句将生成表格

('Baby', ('Stewie', 'Griffin') ) ('Man', ('Peter', 'Griffin') )

【问题讨论】:

  • 您能否更详细地描述所需的结果应该是什么样子?您谈论您想使用但可能根本没有必要使用的概念
  • 并添加样本表数据,和预期的结果!

标签: sql database db2


【解决方案1】:

使用以下 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多