将Oracle结果集复制到MySQL时出现问题

时间:2010-08-13 20:49:46

标签: php mysql oracle

我要做的是将oracle中的查询结果复制到MySQL数据库的表中。这背后的原因并不重要,我不能采取不同的方法。

我正在做的是通过php运行查询,然后将结果复制到oracle中新创建的表中。我知道这不是很有效,但我的表大小非常小,即使查询运行的时间很长。

除了从oracle提起约会之外,其他所有工作都有效;当我按原样运行它时,我的日期字段被设置为0.我正在做的是从oracle检查结果集以查看列的类型是否为“DATE(7)”,如果是,那么我创建了一个MySQL中的列,类型为“DATE”。但由于某些原因,这不起作用。

我正在使用以下代码,抱歉它很长,但似乎最好提供所有。

function hmis_query_transfer ($query_name, $parameters = NULL) {
    //Create Connections to both servers
    $dbConn_Oracle = hmis_oci_connect();
    hmis_mysql_connect("hmis_temp");

    //Retrieve the text for the query and run it on the Oracle server
    $query_Oracle = hmis_query_by_name($query_name, $parameters);
    $stmt_Oracle = hmis_oci_query($dbConn_Oracle , $query_Oracle);

    $ncols = oci_num_fields($stmt_Oracle);

    //Test if table is already created in MySQL
    $mysql_check = "DESC ".$query_name;
    @hmis_mysql_query($mysql_check);
    if (mysql_errno()==1146){
        $mysql_create = "CREATE TABLE ".$query_name." ( ";

        //Transform and append column names to query string
        for ($j = 1; $j <= $ncols; $j++) {
        $name = oci_field_name($stmt_Oracle, $j);
        $type = oci_field_type($stmt_Oracle, $j);
        if ($type == "NUMBER") {
            $type = "INT";
        } else if ($type == "VARCHAR2"){
            $type = "VARCHAR";
        }
        $type .= "(".oci_field_size($stmt_Oracle, $j).")";
        if ($type == "DATE(7)") {
            $type = "DATE";
        }

        $mysql_create .= $name." ".$type.",";
        }
        $mysql_create = substr_replace( $mysql_create, "", -1 );
        $mysql_create .= ')';
        //Create Table
        $result = hmis_mysql_query($mysql_create);
        if ( !$result ){
        die('<strong>Failed Create:</strong>'.mysql_error());
        }
    }
    elseif (!mysql_errno()) {
        //If the table already exists, empty it
        $mysql_truncate = "TRUNCATE TABLE ".$query_name;
        $result = hmis_mysql_query($mysql_truncate);
        if ( !$result ){
        die('<strong>Failed Truncate:</strong>'.mysql_error());
        }
    }

    //Copy over row by row the data from the result set to MySQL
    while ($results_row = oci_fetch_array($stmt_Oracle, OCI_ASSOC )) {
        $mysql_insert = "INSERT INTO ".$query_name." VALUES (";

        for ($i = 1; $i <= $ncols; $i++) {
        $mysql_insert .= "'".$results_row[oci_field_name($stmt_Oracle, $i)]."',";
        }
        $mysql_insert = substr_replace( $mysql_insert, "", -1 );
        $mysql_insert .= ")";
        $result = hmis_mysql_query($mysql_insert);
        if ( !$result ){
        die('<strong>Failed Insert:</strong>'.mysql_error());
        }
    }
}

任何人都可以在我的代码中看到任何缺陷吗?我愿意接受有关如何以不同方式执行此操作的建议,但我希望能够保留我的代码。谢谢你的帮助。


我将数据从oracle复制到MySQL的原因是因为大多数查询需要很长时间才能运行(15-20分钟),并处理大量数据集(2亿),但我的结果集非常小(最多几千)。我的应用程序的构建方式必须在同一个数据集上运行重复查询才能完成它的任务。我本来希望在oracle中创建视图以完成此任务,但我没有权限这样做。因此,我保存了一个中间层结果集并对其进行分析,这恰好是在一台更快的机器上。

1 个答案:

答案 0 :(得分:0)

我不知道Oracle的日期格式,但我会使用strtotime()函数将其转换为unix时间戳,然后使用date()函数将其放入MySQL(YYYY) -MM-DD)格式。只要oracle中的日期字段不是生日或超出UNIX时间戳的范围,这应该可以工作。