2017-10-05 63 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 

希望它會幫助你。所有最好的