DB2无法在存储过程中重新构建物化查询表(MQT)

时间:2014-01-22 20:05:02

标签: stored-procedures db2 refresh materialized-views

我在刷新存储过程中的MQT时遇到问题。

我在尝试刷新表时收到以下错误:

  

ERROR [42601] [IBM] [DB2 / LINUXX8664] SQL0104N意外的令牌" REFRESH"发现在" ANGUAGE SQL BEGIN" 。预期的代币可能包括:" TRUNCATE"

/**********
* CREATE A NEW DUMMY TABLE AND INSERT DUMMY DATA
**********/
CREATE TABLE DELETE_ME (
  COLUMN_A DECIMAL(5)
  );

INSERT INTO DELETE_ME (COLUMN_A) VALUES (1);
INSERT INTO DELETE_ME (COLUMN_A) VALUES (2);
INSERT INTO DELETE_ME (COLUMN_A) VALUES (3);



/**********
* CREATE A MATERIALIZED QUERY USING THE DUMMY TABLE
**********/
CREATE TABLE MQT_TEST AS  (                     
  SELECT COLUMN_A                       
  FROM   DELETE_ME
  ) 
DATA INITIALLY DEFERRED
REFRESH DEFERRED
ENABLE QUERY OPTIMIZATION
NOT LOGGED INITIALLY;
COMMIT;


/**********
* CREATE A SIMPLE PROCEDURE TO REFRESH THE MATERIALIZED QUERY
**********/  
CREATE OR REPLACE PROCEDURE TEST_PROC LANGUAGE SQL
BEGIN
  REFRESH TABLE MQT_TEST;
END; 

--!!!! FAILS WITH THE FOLLOWING ERROR CODE:  - DB2 Database Error : 

ERROR [42601] [IBM][DB2/LINUXX8664] SQL0104N  An unexpected token "REFRESH" was found following "ANGUAGE SQL BEGIN ".  Expected tokens may include:  "TRUNCATE" .

1 个答案:

答案 0 :(得分:2)

您不能在存储过程中将REFRESH TABLE作为静态语句执行,因为它只能作为动态语句运行。

因此,正确的方法是使用EXECUTE IMMEDIATE语句:

CREATE OR REPLACE PROCEDURE TEST_PROC 
   LANGUAGE SQL
BEGIN
  declare vSQL varchar(1024);

  set vSQL = 'refresh table MQT_TEST';
  execute immediate vSQL;

END@