2017-10-05 19 views
1

2つのテーブルの一部を結合したビューを作成したいとします。pl SQLでunion文を使用してビューを作成するときにエラーが発生する

以下は声明である:

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:

私はSQLをPLにはかなり新しいですし、私はの意味を把握することはできません報告されたエラー。

また、最初の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 

それはあなたを助けることを願っています。すべて最高

関連する問題