2017-10-05 95 views
1

我想用两个表的部分联合来创建视图。使用联合声明在pl中创建视图时出错sql

下面是声明:

create view "test" AS 
select 
ppt.reportingtime reportingtime, 
ppt.currency currency, 
ppt.channelid channelid, 
ppt.transactiontype ttype 
FROM preprocessortransactions ppt 
union 
select 
bm.balancetype balancetype 
from balancemovements bm 

该错误消息我得到的是以下几点:

Error starting at line 1 in command:

create view "test" AS SELECT
ppt.reportingtime reportingtime, ppt.currency currency, ppt.channelid channelid, ppt.transactiontype ttype FROM preprocessortransactions ppt union select bm.balancetype balancetype from balancemovements bm

Error at Command Line:1 Column:22 Error report: SQL Error: ORA-01789: query block has incorrect number of result columns 01789. 00000 - "query block has incorrect number of result columns" *Cause:
*Action:

我非常新的PL SQL和我想不通的意义报错。

我还尝试在第一个AS运算符之前列出括号中的列名,但没有成功。

+0

你需要有两个查询的联合编号相同数量的字段。目前,您在顶部有4个字段,底部有1个字段。 – Matt

+0

谢谢你的帮助。为了让工会声明起作用,是否有任何创建空白的方法? – ehammer

+0

你想让bm.balancetype坐在哪个字段? – Matt

回答

0

按照使用NULL的要求插入空白。

CREATE VIEW "test" AS 
SELECT ppt.reportingtime reportingtime, ppt.currency currency, ppt.channelid channelid, ppt.transactiontype ttype 
FROM preprocessortransactions ppt 
UNION 
SELECT NULL, NULL, NULL, bm.balancetype balancetype 
FROM balancemovements bm 
0

在这种情况下工会 & unionall你应该给同桌的属性在这两个查询。

例如:

select a,b,c from test 
union 
--- in this you don't have a,b attribute in your second query, you can use null in their place. 
select '','',c from test 

注意:您可以使用'' or NULL所以您的查询如:

create view "test" 
as 
    select 
     ppt.reportingtime reportingtime, 
     ppt.currency currency, 
     ppt.channelid channelid, 
     ppt.transactiontype ttype,'' 
    from 
     preprocessortransactions ppt 

    union 

    select 
     '', '', '', '', bm.balancetype balancetype 
    from 
     balancemovements bm 

希望它会帮助你。所有最好的