-1

I have a requirement where I have extact the ddl for materialized view and execute it. I have create script for a materialized view as follows :

 CREATE MATERIALIZED VIEW "XXX"."AUD_MV_DCST" ("BUCKET", "ID", "SUM")
  ORGANIZATION HEAP PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS" 
  BUILD IMMEDIATE
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS" 
  REFRESH FAST ON COMMIT
  USING DEFAULT LOCAL ROLLBACK SEGMENT
  USING ENFORCED CONSTRAINTS ENABLE QUERY REWRITE
  AS SELECT TRUNC(STATUS_TIMESTAMP, 'HH24') BUCKET, ID, SUM(1) 
  ..... 

I tried the below replace statement, but it does not work:

select replace(create_script, '''','''''') from dual

What is breaking this is

'HH24')

The opening and closing braces. I get the error:

ORA-00907: missing right parenthesis
ORA-06512: at "XXX.createProc", line 67
ORA-06512: at line 2

Here is the stored procedure that is executing this :

create or replace PROCEDURE procPrintOutput
IS
  -- var declarations ....

  BEGIN

    DECLARE
      -- Get the list of Materialized views.
      CURSOR LIST_OF_MatViews
      IS select *
         from user_mviews
         where MVIEW_NAME = 'TEAM_AUD_MV_NR';
      CURSOR CREATE_MatViews
      IS SELECT *
         FROM temp;
    BEGIN
      FOR Mat_View IN LIST_OF_MatViews
      LOOP
        BEGIN
          DBMS_OUTPUT.PUT_LINE('owner : ' || Mat_View.OWNER || ' - name : ' || Mat_View.MVIEW_NAME);

          -- save the create script of views to temp table.
          l_stmt := 'insert into temp SELECT SYS.DBMS_METADATA.get_ddl (''MATERIALIZED_VIEW'', '''
                    || Mat_View.MVIEW_NAME || ''') from dual';

          EXECUTE IMMEDIATE (l_stmt);
          DBMS_OUTPUT.PUT_LINE('inserted ... ');
          -- drop the view.
          --EXECUTE IMMEDIATE (drop_mat_view_logs_stmt || Mat_View.MVIEW_NAME);
          --DBMS_OUTPUT.PUT_LINE('view log dropped ... ');
        END;
      END LOOP;

      -- recreate view using the create script generated in the previous step.
      FOR MV_CREATE_SCRIPT IN CREATE_MatViews
      LOOP
        BEGIN
          DBMS_OUTPUT.PUT_LINE('SCRIPT : ' || MV_CREATE_SCRIPT.matViewLogQuery);
          replace_stmt := 'select replace(''' || MV_CREATE_SCRIPT.matViewLogQuery || ''','';'','' '') from dual';
          DBMS_OUTPUT.PUT_LINE('replace_stmt : ' || replace_stmt);

          EXECUTE IMMEDIATE (replace_stmt) INTO updated_stmt;
          DBMS_OUTPUT.PUT_LINE('Updated SCRIPT ?? ' || updated_stmt);
          --EXECUTE IMMEDIATE(updated_stmt); 
          --DBMS_OUTPUT.PUT_LINE('view recreated ... ');
        END;
      END LOOP;
    END;
  END;

Any suggestions as to how this can be worked out ??

user1860447
  • 1,316
  • 8
  • 25
  • 46
  • Without context, I'm unsure, but I don't think you want to escape the double quotes. You want single quotes around `HH24` to let Oracle know it is a string. – Shannon Severance Nov 20 '15 at 23:27
  • Yes. I want to escape only the single quotes around HH24 – user1860447 Nov 20 '15 at 23:33
  • Why do you want to escape them? That will make the statement invalid. – Shannon Severance Nov 20 '15 at 23:35
  • I need to execute that create statement in Execute Immediate. I have to drop the view and then recreate it. So before dropping the view I am extracting the create statement using : \nselect dbms_metadata.get_ddl('MATERIALIZED_VIEW', 'AUD_MV_DCST', 'XXX') from dual; \n and using the output of dbms_metadata.get_ddl to recreate the view. – user1860447 Nov 21 '15 at 00:00
  • 2
    Are you copying and pasting the DDL with the quotes replaced into another quoted string? Or are you assigning the value of the DDL with quotes replaced to a variable and passing that variable to `execute immediate`? If the later, don't escape the quotes. And print the value that is being passed to `execute immediate` for debugging. If the former, then look at your output, and get it correct and then work back to what code changes you need. – Shannon Severance Nov 21 '15 at 00:21
  • 2
    If you're putting the result of the dbms_metadata into a varchar2 variable then you don't need to do anything. The quotes only need to be escaped in a literal. What happens when you just run it unaltered? And if you are copying and pasting, (a) why would you use dynamic SQL anyway, and (b) look into the alternatve quoted syntax. – Alex Poole Nov 21 '15 at 00:22
  • When I try to run it unaltered this is the error I get :\nError report - ORA-00907: missing right parenthesis ORA-06512: at "XXX.PROCPRINTOUTPUT", line 67 ORA-06512: at line 1 00907. 00000 - "missing right parenthesis" *Cause: *Action: – user1860447 Nov 21 '15 at 00:37
  • When you execute MV_CREATE_SCRIPT.matViewLogQuery directly, without modifying it at all? – Alex Poole Nov 21 '15 at 00:49
  • I have to replace the ';' that the create script comes with. That is what the replace_stmt is doing there. If I do not replace the ';', then it errors out. This query runs fine with there views. Errors occur only for the views that have 'HH24') – user1860447 Nov 21 '15 at 00:56

1 Answers1

0

I tried the following and had no issues:

SQL> select trunc(current_timestamp, 'HH24') BUCKET from dual;

BUCKET
------------------
20-NOV-15

SQL>

Try just leaving off the precision specifier and use:

SQL> select trunc(current_timestamp) BUCKET from dual;

BUCKET
------------------
20-NOV-15

SQL>

HTH

tale852150
  • 1,618
  • 3
  • 17
  • 23
  • Oh, yeah there is that. Dates and timestamps truncate to the day be default. – Shannon Severance Nov 21 '15 at 00:22
  • 1
    With a different format model you'd see [they are not the same](http://sqlfiddle.com/#!4/9eecb7d/11552). The default format for trunc is DD. – Alex Poole Nov 21 '15 at 00:48
  • That will not work since 'HH24') it is part of a script that is returned. – user1860447 Nov 21 '15 at 01:47
  • Good points Alex Poole and user1860447, but I was wondering why the user needed to truncate only the 'hour'. So (I suggested) a simple trunc() might be the only thing the user really needs (an assumption I realize). Looks like we now have the context from the user in the form of the stored procedure so this will need another look. – tale852150 Nov 23 '15 at 01:34