PL / SQL过程没有返回预期的结果

时间:2014-05-22 14:01:38

标签: oracle shell plsql sqlplus

我有一个PL / SQL过程,我在shell脚本中运行。

我把逻辑拉出来但它没有运行内部BEGIN-END;部分:

DECLARE
model_exists NUMBER(1);
BEGIN 
  SELECT COUNT(*) INTO model_exists from mdsys.rdf_model$ WHERE model_name='XCLOVER';
  BEGIN 
      EXECUTE IMMEDIATE 'CREATE table SEMANTIC.RDF_STAGE_TABLE_XCLOVER(RDF$STC_sub  VARCHAR(4000) not null,RDF$STC_pred VARCHAR(4000) not null,RDF$STC_obj VARCHAR(4000) not null)'; COMMIT;
      EXECUTE IMMEDIATE 'CREATE TABLE SEMANTIC.XCLOVER_TPL (TRIPLE SDO_RDF_TRIPLE_S)'; COMMIT;
      EXCEPTION
      WHEN OTHERS THEN
      IF SQLCODE != -955 THEN
        EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.XCLOVER_TPL';
        EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.RDF_STAGE_TABLE_XCLOVER';
      END IF;
  END; 
EXECUTE IMMEDIATE 'GRANT ALL ON SEMANTIC.XCLOVER_TPL to MDSYS'; 
IF model_exists = 0 THEN
    SEM_APIS.CREATE_SEM_MODEL('XCLOVER', 'XCLOVER_TPL','TRIPLE'); 
END IF;
COMMIT; 
END;
/

在shell脚本中编写的方式如下:

#!/bin/bash
. /etc/profile.d/oracle.sh

MODEL=$1

echo "DECLARE" > createxmodel.tmp
echo "model_exists NUMBER(1);" >> createxmodel.tmp
echo "BEGIN" >> createxmodel.tmp
echo "SELECT COUNT(*) INTO model_exists from mdsys.rdf_model\$ WHERE model_name='$MODEL';" >> createxmodel.tmp
echo "BEGIN" >> createxmodel.tmp
echo "EXECUTE IMMEDIATE 'CREATE table SEMANTIC.RDF_STAGE_TABLE_$MODEL(RDF\$STC_sub  VARCHAR(4000) not null,RDF\$STC_pred VARCHAR(4000) not null,RDF\$STC_obj VARCHAR(4000) not null)';" >> createxmodel.tmp
echo "EXECUTE IMMEDIATE 'CREATE TABLE SEMANTIC.$MODEL""_TPL (TRIPLE SDO_RDF_TRIPLE_S)'; " >> createxmodel.tmp
echo "EXCEPTION" >> createxmodel.tmp
echo "WHEN OTHERS THEN" >> createxmodel.tmp
echo "IF SQLCODE != -955 THEN" >> createxmodel.tmp
echo "EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.$MODEL""_TPL';" >> createxmodel.tmp
echo "EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.RDF_STAGE_TABLE_$MODEL';" >> createxmodel.tmp
echo "END IF;" >> createxmodel.tmp
echo "END;" >> createxmodel.tmp
echo "EXECUTE IMMEDIATE 'GRANT ALL ON SEMANTIC.$MODEL""_TPL to MDSYS';" >> createxmodel.tmp 
echo "IF model_exists = 0 THEN" >> createxmodel.tmp
echo "SEM_APIS.CREATE_SEM_MODEL('$MODEL', '$MODEL""_TPL','TRIPLE');" >> createxmodel.tmp
echo "END IF;" >> createxmodel.tmp
echo "COMMIT;" >> createxmodel.tmp
echo "END;" >> createxmodel.tmp
echo "/" >> createxmodel.tmp
echo "exit;" >> createxmodel.tmp

sqlplus -S user/password < createxmodel.tmp

它现在抛出的错误是:

DECLARE
*
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-06512: at line 15

告诉我内部BEGIN永远不会被执行......

这种用法:(

有人看到我做错了吗?

3 个答案:

答案 0 :(得分:1)

您需要做的就是在创建后移动GRANT语句 - 没有理由将其置于块之外 - 您获得的错误来自编辑器,而不是实际的PL / SQL错误,对吗?

    DECLARE
    model_exists NUMBER(1);
    BEGIN 
      SELECT COUNT(*) INTO model_exists from mdsys.rdf_model$ WHERE model_name='XCLOVER';
      BEGIN 
          EXECUTE IMMEDIATE 'CREATE table SEMANTIC.RDF_STAGE_TABLE_XCLOVER(RDF$STC_sub  VARCHAR(4000) not null,RDF$STC_pred VARCHAR(4000) not null,RDF$STC_obj VARCHAR(4000) not null)'; COMMIT;
          EXECUTE IMMEDIATE 'CREATE TABLE SEMANTIC.XCLOVER_TPL (TRIPLE SDO_RDF_TRIPLE_S)'; COMMIT;

EXECUTE IMMEDIATE 'GRANT ALL ON SEMANTIC.XCLOVER_TPL to MDSYS';
          EXCEPTION
          WHEN OTHERS THEN
          IF SQLCODE != -955 THEN
            EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.XCLOVER_TPL';
            EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.RDF_STAGE_TABLE_XCLOVER';
          END IF;
      END; 

    IF model_exists = 0 THEN
        SEM_APIS.CREATE_SEM_MODEL('XCLOVER', 'XCLOVER_TPL','TRIPLE'); 
    END IF;
    COMMIT; 
    END;

答案 1 :(得分:1)

我认为你最好把每个表的创建放在它自己的BEGIN ... END块中,如:

echo "DECLARE" > createxmodel.tmp
echo "  model_exists NUMBER(1);" >> createxmodel.tmp
echo "  excp_table_exists EXCEPTION;" >> createxmodel.tmp
echo "  PRAGMA EXCEPTION_INIT(excp_table_exists, -955);" >> createxmodel.tmp
echo "BEGIN" >> createxmodel.tmp
echo "  SELECT COUNT(*) INTO model_exists from mdsys.rdf_model\$ WHERE model_name='$MODEL';" >> createxmodel.tmp

echo "  BEGIN" >> createxmodel.tmp
echo "    EXECUTE IMMEDIATE 'CREATE table SEMANTIC.RDF_STAGE_TABLE_$MODEL(RDF\$STC_sub  VARCHAR(4000) not null,RDF\$STC_pred VARCHAR(4000) not null,RDF\$STC_obj VARCHAR(4000) not null)';" >> createxmodel.tmp
echo "  EXCEPTION" >> createxmodel.tmp
echo "    WHEN excp_table_exists THEN" >> createxmodel.tmp
echo "      EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.RDF_STAGE_TABLE_$MODEL';" >> createxmodel.tmp
echo "  END;" >> createxmodel.tmp
echo "  BEGIN" >> createxmodel.tmp
echo "    EXECUTE IMMEDIATE 'CREATE TABLE SEMANTIC.$MODEL""_TPL (TRIPLE SDO_RDF_TRIPLE_S)'; " >> createxmodel.tmp
echo "  EXCEPTION" >> createxmodel.tmp
echo "    WHEN excp_table_exists THEN" >> createxmodel.tmp
echo "      EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.$MODEL""_TPL';" >> createxmodel.tmp
echo "  END;" >> createxmodel.tmp
echo "  EXECUTE IMMEDIATE 'GRANT ALL ON SEMANTIC.$MODEL""_TPL to MDSYS';" >> createxmodel.tmp 
echo "  IF model_exists = 0 THEN" >> createxmodel.tmp
echo "    SEM_APIS.CREATE_SEM_MODEL('$MODEL', '$MODEL""_TPL','TRIPLE');" >> createxmodel.tmp
echo "  END IF;" >> createxmodel.tmp
echo "  COMMIT;" >> createxmodel.tmp
echo "END;" >> createxmodel.tmp
echo "/" >> createxmodel.tmp
echo "exit;" >> createxmodel.tmp

希望这会有所帮助。

分享并享受。

答案 2 :(得分:0)

这是为什么正确的异常处理很重要的一个例子。 可能如果你至少有一个dbms_output,那么很多headasche都会被保存:

DECLARE
model_exists NUMBER(1);
BEGIN 
  SELECT COUNT(*) INTO model_exists from mdsys.rdf_model$ WHERE model_name='XCLOVER';
  BEGIN 
      EXECUTE IMMEDIATE 'CREATE table SEMANTIC.RDF_STAGE_TABLE_XCLOVER(RDF$STC_sub  VARCHAR(4000) not null,RDF$STC_pred VARCHAR(4000) not null,RDF$STC_obj VARCHAR(4000) not null)'; COMMIT;
      EXECUTE IMMEDIATE 'CREATE TABLE SEMANTIC.XCLOVER_TPL (TRIPLE SDO_RDF_TRIPLE_S)'; COMMIT;
      EXCEPTION
      WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('Some error has occurred'):
      IF SQLCODE != -955 THEN
        EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.XCLOVER_TPL';
        EXECUTE IMMEDIATE 'TRUNCATE TABLE SEMANTIC.RDF_STAGE_TABLE_XCLOVER';
      END IF;
  END; 
EXECUTE IMMEDIATE 'GRANT ALL ON SEMANTIC.XCLOVER_TPL to MDSYS'; 
IF model_exists = 0 THEN
    SEM_APIS.CREATE_SEM_MODEL('XCLOVER', 'XCLOVER_TPL','TRIPLE'); 
END IF;
COMMIT; 
END;
/