问题
I have two different tables that hold two different sets of report data. Yet I want to have a UNION ALL view so I can display the common columns together. I first tried making a separate column:
SELECT NVL(ROW_NUMBER() OVER (ORDER BY REPORT_PERIOD DESC, REPORT_ID DESC),0) ROW_ID,u.REPORT_ID,u."REPORT_PERIOD",u."EXCEL_DOC",u."XML_DOC",u."STATUS",u."CREATED_BY",u."CREATED_ON",u."MODIFIED_BY",u."MODIFIED_ON"
FROM
(
SELECT ('DR' || TO_CHAR(DISTRIBUTION_REPORT_ID)) AS REPORT_ID,
REPORT_PERIOD,
EXCEL_DOC,
XML_DOC,
STATUS,
CREATED_BY,
CREATED_ON,
MODIFIED_BY,
MODIFIED_ON
FROM DISTRIBUTION_REPORT
UNION ALL
SELECT ('TR' || TO_CHAR(TREATMENT_REPORT_ID)) AS REPORT_ID,
REPORT_PERIOD,
EXCEL_DOC,
XML_DOC,
STATUS,
CREATED_BY,
CREATED_ON,
MODIFIED_BY,
MODIFIED_ON
FROM TREATMENT_REPORT
) u
Visual Studio would not let me add that because it could not infer a primary key. So then I added one:
ALTER VIEW VW_ALL_REPORT ADD CONSTRAINT VW_ALL_REPORT_PK PRIMARY KEY (ROW_ID) DISABLE
I then got an error saying that all parts of the key must be non-nullable. I then tried adding a rownumber() column and tried the same things, and got the same exact errors.
In each case, Oracle seems to think that the key column is nullable, even though it shouldn't if it were half way intelligent. So it passes on this inaccuracy to Visual Studio.
If you double click the view you will get the properties for each column. Notice "Nullable = Yes"
I realize I could add a third table called "ALL_REPORT" and could just add rows for each row in the other two tables. However, this really seems like a waste of resources. Any ideas?
I am using VS 2013, Oracle 11g, and VB.NET.
回答1:
I have no idea what nullability would have to do with creating a view.
On the other hand, this code should not work in Oracle:
SELECT ('DR' + TO_CHAR(DISTRIBUTION_REPORT_ID)) AS REPORT_ID,
The correct formulation is:
SELECT ('DR' || TO_CHAR(DISTRIBUTION_REPORT_ID)) AS REPORT_ID,
+
is an arithmetic operator that doesn't work well on strings.
来源:https://stackoverflow.com/questions/45423920/import-oracle-union-all-view-into-entity-framework-edmx