【问题标题】:Error when creating view with union statement in pl sql在 pl sql 中使用 union 语句创建视图时出错
【发布时间】:2018-03-16 22:24:55
【问题描述】:

我想创建一个合并两个表的部分的视图。

以下是声明:

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 报告时间, ppt.currency 货币, ppt.channelid channelid, ppt.transactiontype ttype 从 预处理器事务ppt 联盟 选择 bm.balancetype balancetype 来自 balancemovements bm

命令行错误:1 列:22 错误报告:SQL 错误:ORA-01789:查询块的结果列数不正确 01789。00000 -“查询块的结果列数不正确” *原因:
*行动:

我对pl sql很陌生,我无法弄清楚报告的错误的含义。

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

【问题讨论】:

  • 对于两个联合查询的查询,您需要具有相同数量的字段。目前,您在顶部有 4 个字段,在底部有 1 个字段。
  • 感谢您的帮助。有什么方法可以创建空白以使联合声明起作用?
  • 您希望 bm.balancetype 位于哪个字段下?
  • "transactiontype" 如你所料。非常感谢您的意见
  • 编辑您的问题并提供示例数据和所需的结果。

标签: sql oracle ddl


【解决方案1】:

使用NULL's 按要求插入空格。

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

【讨论】:

    【解决方案2】:

    在这种情况下 unionunionall 您应该在两个查询中提供相同的表属性。

    例如:

    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
    

    希望对您有所帮助。万事如意

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多