在pl sql中使用union语句创建视图时出错

时间:2017-10-05 10:20:28

标签: sql oracle ddl

我想创建一个包含两个表的部分联合的视图。

以下是声明:

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

我收到的错误消息如下:

  

在命令的第1行开始出错:

     

创建视图"测试" AS SELECT
      ppt.reportingtime reportingtime,       ppt.currency货币,       ppt.channelid channelid,       ppt.transactiontype ttype       从       preprocessortransactions ppt       联盟       选择       来自balancemovements bm的bm.balancetype balancetype

     

命令行出错:1列:22错误报告:SQL错误:ORA-01789:查询块的结果列数不正确01789. 00000 - "查询块的结果列数不正确"       *原因:
      *操作:

我是pl sql的新手,我无法弄清楚报告错误的含义。

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

希望它会对你有所帮助。一切顺利