Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在DB2 with子句中使用用户定义的表类型?_Sql_Database_Db2 - Fatal编程技术网

Sql 如何在DB2 with子句中使用用户定义的表类型?

Sql 如何在DB2 with子句中使用用户定义的表类型?,sql,database,db2,Sql,Database,Db2,我在DB2中创建了一个用户定义的表类型,如下所示: create table fullname as (street varchar(100), addr varchar(100)) WITH result (one, fullname) as ( select one, two, three from info UNION ALL select one, two three from other_info ) 我想知道如何在WITH子句中使用它来基本上将子表列添加到

我在DB2中创建了一个用户定义的表类型,如下所示:

create table fullname as (street varchar(100), addr varchar(100))
WITH result (one, fullname) as
(
    select one, two, three from info
    UNION ALL
    select one, two three from other_info
)
我想知道如何在WITH子句中使用它来基本上将子表列添加到此udtt。WITH条款如下:

create table fullname as (street varchar(100), addr varchar(100))
WITH result (one, fullname) as
(
    select one, two, three from info
    UNION ALL
    select one, two three from other_info
)
我想将第二列和第三列分组为一列,由fullname表类型表示。这可能吗?它是如何做到的

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

信息一个varchar50,两个varchar50,三个varchar50

其他信息一个varchar50,两个varchar50,三个varchar50

因此WITH子句将生成一个表,该表是info和其他_info表的并集,并且该生成的表将具有模式

结果1 varchar50,全名

其中,fullname是用户定义的表类型,它包含来自union的两个列属性2和3,作为一列

因此,如果表信息包含:

“男人”、“彼得”、“格里芬”

以及包含其他信息的表格:

“宝贝”、“斯图伊”、“格里芬”

然后WITH子句将生成表

“宝贝”、“斯图伊”、“格里芬”
“Man”、“Peter”、“Griffin”使用以下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.

你能更详细地描述一下期望的结果是什么样的吗?您将讨论您想要使用的概念,但这些概念可能根本不是必需的,并添加示例表数据和预期结果!