68

Iam trying to refresh the materialized view by using:

DBMS_MVIEW.REFRESH('v_materialized_foo_tbl')

But it's throwing invalid sql statement.

Then I have created a stored procedure like this:

CREATE OR REPLACE 
PROCEDURE MAT_VIEW_FOO_TBL 
IS
BEGIN
   DBMS_MVIEW.REFRESH('v_materialized_foo_tbl')
END MAT_VIEW_FOO_TBL IS;

This procedure has been created successfully but when i am calling this procedure with

MAT_VIEW_FOO_TBL;

it's throwing an error again.

Kindly suggest a solution for this issue.

Thanks, Srinivas

Ollie
  • 17,058
  • 7
  • 48
  • 59
Srinivas
  • 1,516
  • 3
  • 17
  • 27

9 Answers9

69

Run this script to refresh data in materialized view:

BEGIN
DBMS_SNAPSHOT.REFRESH('Name here');
END;
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
Waqas Ali
  • 1,441
  • 2
  • 20
  • 25
  • 1
    Above code is tested various times, and it works fine, no exception/error. There may be some problem with your tool/mechane etc. – Waqas Ali Oct 02 '13 at 10:46
  • 3
    If you have a schema, use the following `dbms_snapshot.refresh('schema.view');` – Zeus Jul 18 '19 at 13:25
57

try this:

DBMS_SNAPSHOT.REFRESH( 'v_materialized_foo_tbl','f'); 

first parameter is name of mat_view and second defines type of refresh. f denotes fast refresh. but keep this thing in mind it will override any any other refresh timing options.

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
fahim ashraf
  • 594
  • 6
  • 3
  • 4
    This works fine in an IDE like SQL Developer, but if you are executing it from code (like ODP.NET etc..) then it has to be wrapped in BEGIN & END as @Waqas Ali suggests. – Tom Halladay Apr 22 '14 at 05:50
  • 1
    @TomHalladay Is there something wrong with using `EXECUTE` for that? (The raw function certainly didn't work from SQL Developer for me.) – jpmc26 Jun 17 '14 at 08:46
  • Getting below error: REFRESH FAST can not be used for materialized views – Subhadeep Ray Jun 30 '22 at 09:36
18

a bit late to the game, but I found a way to make the original syntax in this question work (I'm on Oracle 11g)

** first switch to schema of your MV **

EXECUTE DBMS_MVIEW.REFRESH(LIST=>'MV_MY_VIEW');

alternatively you can add some options:

EXECUTE DBMS_MVIEW.REFRESH(LIST=>'MV_MY_VIEW',PARALLELISM=>4);

this actually works for me, and adding parallelism option sped my execution about 2.5 times.

More info here: How to Refresh a Materialized View in Parallel

Sonic Soul
  • 23,855
  • 37
  • 130
  • 196
8

You can refresh a materialized view completely as follows:

EXECUTE  
DBMS_SNAPSHOT.REFRESH('Materialized_VIEW_OWNER_NAME.Materialized_VIEW_NAME','COMPLETE');
Serban Tanasa
  • 3,592
  • 2
  • 23
  • 45
Yasir Meraj
  • 81
  • 1
  • 1
8

Best option is to use the '?' argument for the method. This way DBMS_MVIEW will choose the best way to refresh, so it'll do the fastest refresh it can for you. , and won't fail if you try something like method=>'f' when you actually need a complete refresh. :-)

from the SQL*Plus prompt:

EXEC DBMS_MVIEW.REFRESH('my_schema.my_mview', method => '?');
mike
  • 2,149
  • 20
  • 29
5

If you're working with SQL Developer, you have to put the dbms_view in lowercase. The rest compiled fine for me although I haven't called the procedure from code yet.

CREATE OR REPLACE PROCEDURE "MAT_VIEW_FOO_TBL" AS 
BEGIN
  dbms_mview.refresh('v_materialized_foo_tbl');
END;
Will Lovett
  • 1,241
  • 3
  • 18
  • 35
1

Try using the below syntax:

Common Syntax:

begin
dbms_mview.refresh('mview_name');
end;

Example:

begin
dbms_mview.refresh('inv_trans');
end;

Hope the above helps.

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
1

EXECUTE dbms_mview.refresh('view name','cf');

issam
  • 93
  • 3
-1

When we have to use inbuilt procedures or packages we have to use "EXECUTE" command then it will work.

EX:

EXECUTE exec DBMS_MVIEW.REFRESH('v_materialized_foo_tbl');

moses
  • 1
  • 4
    Welcome to Stackoverflow. Please take some time to read how to write a good answer. read [this](http://meta.stackexchange.com/a/7659/338114) and [this](http://stackoverflow.com/help/how-to-answer) – Sourav Ghosh Feb 09 '17 at 07:10