错误备份表/创建表使用过程和codeigniter

时间:2018-01-08 10:13:41

标签: oracle codeigniter plsql

我正在尝试在oracle和codeigniter上备份我当前的表使用存储过程。

这是我的程序:

CREATE OR  PROCEDURE PROC_BACKUP_DATA1 (p_tablename  VARCHAR2, out_message 
OUT VARCHAR2)
AS
BEGIN
execute immediate 'create table ROADMAP_'|| to_char(sysdate,'yyyyMMddHH24miss') || 'as Select * from ' || p_tablename||'';
commit;
out_message := 'Data berhasil di backup';                               
exception
when others then
rollback;
out_message := sqlerrm;


DBMS_OUTPUT.PUT_LINE(out_message);
END;

这是我的CI模型代码:

public function fbackup(){
$p_tablename="ZZZ_REKAP_ROADMAP";
$stid=oci_parse($this->db->conn_id, 'begin PROC_BACKUP_DATA1(:p_tablename, :out_message); end;');
oci_bind_by_name($stid, ':p_tablename', $p_tablename);
oci_bind_by_name($stid, ':out_message', $out_message, 40);
oci_execute($stid);
print"$out_message\n";
oci_free_statement($stid);
oci_close($this->db->conn_id);
}

这是我的CI控制器:

public function backup(){
$this->RoadmapModel->fbackup();
$this->load->view('index');
}

这也是错误消息:

  

ORA-00922:缺少或无效选项

我尽我所能,并且堆叠 任何人都有解决方案吗?

谢谢兄弟......

1 个答案:

答案 0 :(得分:0)

Procedure后,您创建的replace语法缺少OR。见这里:

  

CREATE OR PROCEDURE PROC_BACKUP_DATA1(p_tablename VARCHAR2,   out_message

见下面的工作版本:

CREATE OR REPLACE PROCEDURE PROC_BACKUP_DATA1 (
     P_TABLENAME         VARCHAR2,
     OUT_MESSAGE   OUT   VARCHAR2)
AS
BEGIN
     EXECUTE IMMEDIATE    'create table ROADMAP_'
                       || TO_CHAR (SYSDATE, 'yyyyMMddHH24miss')
                       || ' as Select * from '
                       || P_TABLENAME
                       || '';

     OUT_MESSAGE := 'Data berhasil di backup';
EXCEPTION
     WHEN OTHERS THEN
          ROLLBACK;
          OUT_MESSAGE := SQLERRM;
          DBMS_OUTPUT.PUT_LINE (OUT_MESSAGE);
END;

还要注意@Alex对异常处理的看法。简单地将dbms_output伤口处理为exception并且程序将执行successfully,即使出现错误。