【问题标题】:How to drop and create Oracle Database tables in PHP如何在 PHP 中删除和创建 Oracle 数据库表
【发布时间】:2020-03-28 16:57:53
【问题描述】:

我的代码创建了包含 13 个表的所有数据库。

我想增加删除这些表的可能性(已经得到这个)并将它们添加为新的和空的。

我的代码:

include 'connection.php';

$c = oci_connect($username, $password, $database);
if (!$c) {
    $m = oci_error();
    trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
}

$createDb = "CREATE TABLE adres (
    adresid        INTEGER NOT NULL,
    miasto         VARCHAR2(50 CHAR),
    ulica          VARCHAR2(50 CHAR),
    kod_pocztowy   CHAR(11 CHAR),
    nr_mieszkania  CHAR(5 CHAR),
    nr_domu        CHAR(5 CHAR)
);";

$ex = oci_parse($c,$createDb);
oci_execute($ex);

作为回应,我收到了警告:

Warning: oci_execute(): ORA-00922: missing or invalid option

【问题讨论】:

  • 尝试删除 create 语句中的分号
  • 这适用于一张桌子。但是我可以在一个 $createDb 变量中包含所有查询吗?
  • @JoffreySchmitz 不适用于创建表
  • 作为提示,这里有一个常用技巧:begin execute immediate 'drop table TestTempTable'; exception when others then if sqlcode <> -942 then raise; end if; end;

标签: php oracle oracle-call-interface


【解决方案1】:

您可以将所有创建语句放入表 temp1 中,您可以使用 oci_parse 和 oci_execute 创建和插入该表 然后您可以使用 PLSQL 块使用 oci_parse 和 oci_execute 运行您的代码。 此外,在需要的地方删除分号。

第一步

        CREATE TABLE temp1
      (
         id       NUMBER,
         sql_text CLOB
      );

第二步

    BEGIN
    INSERT INTO temp1
    VALUES     (1,
                'drop table t2 ;drop table t3;');
    INSERT INTO temp1
    VALUES     (2,
                'create table t2 (t number);create table t3 (t1 number);');
    END;

第三步

            BEGIN
        FOR rec IN (SELECT blk.id,
                           TRIM(Regexp_substr(sql_text, '[^;]+', 1, lvl)) sql_text
                    FROM   (SELECT DISTINCT id,
                                            LEVEL lvl
                            FROM   temp1
                            CONNECT BY LEVEL <= Regexp_count(Trim(sql_text), '[^;]+'
                                                ))
                           blk,
                           temp1
                    WHERE  blk.id = temp1.id
                    ORDER  BY blk.id,
                              lvl) LOOP
            BEGIN
                EXECUTE IMMEDIATE rec.sql_text;
            EXCEPTION
                WHEN OTHERS THEN
                  dbms_output.Put_line(SQLERRM);
            END;
        END LOOP;
    END; 

或者您可以将所有三个组合成一个 PLSQL 块并调用 oci_parse 和 oci_execute

                BEGIN
        EXECUTE IMMEDIATE 'DROP TABLE temp1';

        EXECUTE IMMEDIATE ' CREATE TABLE temp1
              (
                 id       NUMBER,
                 sql_text CLOB
              )';

        INSERT INTO temp1
        VALUES      (1,
                     'drop table t2 ;drop table t3;');

        INSERT INTO temp1
        VALUES      (2,
                     'create table t2 (t number);create table t3 (t1 number);');


        FOR rec IN (SELECT blk.id,
                           TRIM(Regexp_substr(sql_text, '[^;]+', 1, lvl)) sql_text
                    FROM   (SELECT DISTINCT id,
                                            LEVEL lvl
                            FROM   temp1
                            CONNECT BY LEVEL <= Regexp_count(Trim(sql_text), '[^;]+'
                                                ))
                           blk,
                           temp1
                    WHERE  blk.id = temp1.id
                    ORDER  BY blk.id,
                              lvl) LOOP
            BEGIN
                EXECUTE IMMEDIATE rec.sql_text;
            EXCEPTION
                WHEN OTHERS THEN
                  dbms_output.Put_line(SQLERRM);
            END;
        END LOOP;
    END; 

【讨论】:

    猜你喜欢
    • 2015-06-28
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多