PHP,用于将 db2 数据从一个数据库移动到另一个数据库的脚本

我需要将开发数据库上的表中的一些数据移植到生产数据库上的相同表中,但生产数据库已经具有与开发数据库匹配的主键记录,因此我无法使用主键bundleRenderer.renderToStream转储数据


在本例中, 是父记录中的主键,用于将子记录与其相关联。执行父记录的插入将创建一个新的主键,因此我需要子插入也具有新创建的主键,以便在生产数据库bundleRenderer.renderToStream 上维护关系item_id


到目前为止,我的脚本:


<?php

$DB2connPROD = odbc_connect("schema","user", "pass");

$DB2connDEV = odbc_connect("schema","user", "pass");



//Get itemt records from dev


$getDevitems = "

    select item_id,item_typet_id,item_identifier,expiration_timestamp 

    from development.itemt where item_typet_id in (2,3)

";


//$getDevitems will get records that have a primary key item_id which is used to get the records in the following select queries


foreach($getDevitems as $items){


    //Get all comments


    $getComments = "

    select tc.item_id, tc.comment, tc.comment_type_id from development.item_commentt tc

    inner join development.itemt t on tc.item_id = t.item_id

    where t.item_id = {item_id_from_getDevitems}

    ";


    $insertitem = "INSERT into production (item_identifier,expiration_timestamp)

    values (item_identifier,expiration_timestamp)";


    $insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)

    values (item_id, comment, comment_type_id)";


}


?>

因此,如果$getDevitems返回


item_id  |  item_typet_id  |  item_identifier  |  expiration_timestamp

----------------------------------------------------------------------------

123             1                   544                 '2020-03-01 12:00:00'

我希望它现在运行注释选择,其中123作为 where 子句中的 ID:


select tc.item_id, tc.comment, tc.comment_type_id from development.item_commentt tc

inner join development.itemt t on tc.item_id = t.item_id

where t.item_id = 123

现在,对于我的旧父记录,我拥有所有父数据和所有关系子数据。因此,我想将新的父记录插入到数据库中,创建新ID,并使用新创建的主键/ ID插入子记录。因此,对于新的父记录,我会这样做:


$insertitem = "INSERT into production (item_identifier,expiration_timestamp)

values (544,'2020-03-01 12:00:00')";


Bottom LIne:我需要从开发数据库中获取关系数据(全部基于item_id),并将其插入到一个新的数据库中,该数据库会创建一个新的主键,但我需要保持这种关系。


如何正确完成此操作以执行我需要的工作,并确保我保持每个最初选择的项目的完整关系?


浮云间
浏览 319回答 2
2回答

繁星coding

鉴于您的插入件是:$insertitem = "INSERT into production (item_identifier,expiration_timestamp)values (item_identifier,expiration_timestamp)";$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)values (item_id, comment, comment_type_id)";看起来您正在使用标识列来item_id。您可以使用该函数检索最近生成的标识值,因此第二个插入应为:IDENTITY_VAL_LOCAL()$insertComment = "INSERT into productionComment (item_id, comment, comment_type_id)values (IDENTITY_VAL_LOCAL(), comment, comment_type_id)";

烙印99

我无法帮助PHP,但是对于IBMi的DB2,您有不同的解决方案:如果我理解正确,item_id是一个生成总是作为标识列您可以使用以下命令获取新创建的item_idselect item_id from final table (&nbsp; INSERT into production (item_identifier,expiration_timestamp)&nbsp; values (544,'2020-03-01 12:00:00'))或者,您可以使用开发值或您自己的增量强制item_id的值&nbsp; INSERT into production (idtem_id, item_identifier,expiration_timestamp)&nbsp; values (<your value>, 544,'2020-03-01 12:00:00')&nbsp; OVERRIDING SYSTEM VALUE在这种情况下,您必须通过发出为item_id设置下一个值alter table production alter column item_id restart with <restart value>
打开App,查看更多内容
随时随地看视频慕课网APP