I have a package where I need to do similar unions many times . This package is executed every day and I need evey day different data(depending on day). So I though I create a temp view and do operations on this view. As it it seems not possible to create view in a procedure in a static way and it is not possible to use bind variables I tried to do it in this way:
PROCEDURE CREATE_VIEW( from_date DATE) IS
sqlCommand VARCHAR2(32000);
BEGIN
sqlCommand :=
'CREATE VIEW TMP_HELPER_VIEW AS
SELECT ID, IMPORT1_ID, IMPORT2_ID, PROD_ID
FROM
(
SELECT ID,
IMPORT1_ID,
-1,
PROD_ID
FROM TABLE1
WHERE
TS >= '||from_date||'
AND TS < ADD_MONTHS('||from_date||', 1)
UNION
SELECT ID,
-1,
IMPORT2_ID,
PROD_ID
FROM TABLE2
WHERE
TS >= '||from_date||'
AND TS < ADD_MONTHS('||from_date||', 1)';
EXECUTE IMMEDIATE sqlCommand;
END CREATE_VIEW;
However I'm getting here ORA-00907: missing right parenthesis .
I think I'm concateing date variable in a wrong way. Can someone give me a clue how can I fix it?
Many thanks!
orcondition or by pipelined functionor: Oracle's optimizer is smart enough to distinguish constants, so it will not compare each null coming from the table with absent column, but just skip that table. See db<>fiddle: it accesses required partitions.UNION ALL. When you have indexes on columnTSthen the view should not bring any performance improvement.