简体   繁体   English

DB2根据php脚本中的绑定参数进行选择和插入

[英]DB2 Select and insert based on bound parameters in php script

I've been trying to slowly migrate a script that selects data, and inserts based on bound parameters into another table from mysql to db2. 我一直在尝试缓慢地迁移一个脚本,该脚本选择数据,并将基于绑定参数的脚本插入到另一个表中,从mysql到db2。

I have most of it migrated, but this main portion is still failing to insert. 我大部分已迁移,但是主要部分仍然无法插入。 My select is working, returning exactly what I expect. 我的选择正在工作,完全返回了我的期望。 However, something is going wrong in the bit where I create the array or parameter values. 但是,在创建数组或参数值的地方出了点问题。 I"m simply trying to iterate through the selected value rows and insert the values into a matching table. 我只是试图遍历所选值行并将这些值插入匹配表中。

I'm I using odbc_fetch_array incorrectly, or does it look like something's wrong with my bound parameters? 我使用的odbc_fetch_array不正确,或者绑定参数看起来有问题吗?

//Main query to select data
$data = "
  SELECT  
      u.extension
      , sum(duration) as total_talk_time_seconds
      , round(sum(duration) / 60,2) as total_talk_time_minutes
      , sum(case when legtype1 = 1 then 1 else 0 end) as total_outbound

  from SESSION a
    join call_summary b
      on a.notablecallid = b.notablecallid
    inner join system_USERS u
      on u.EXTENSION = CALLINGPARTYNO or u.EXTENSION = FINALLYCALLEDPARTYNO
  group by extension,u.user_id" or die(db2_conn_error($DB2Conn));


$stmt = "
  INSERT into daily_call_totals
    (extension,
    total_talk_time_seconds,
    total_talk_time_minutes,
    total_outbound)
  VALUES (?, ?, ?, ?)" or die(db2_conn_error($DB2Conn));

//create array for binding
$content = [];

$mainResult = odbc_exec($DB2Conn, $data);
while ($d = odbc_fetch_array($mainResult)) {

  $prepInsert = odbc_prepare($DB2Conn, $stmt);

  //for each row, bind param. This is to ensure we get the correct number of records whether they're being inserted or updated for duplicates
  $values = [
      $d['extension'],
      $d['total_talk_time_seconds'],
      $d['total_talk_time_minutes'],
      $d['total_outbound']];

  // Store the current row
  $content[] = $d;

  if($prepInsert){
      $result = odbc_execute($prepInsert,$values);
      if($result){
        print "successfully added record";
      }
    }
}

As mentioned consider an INSERT...SELECT and avoid the looped INSERT...VALUES . 如前所述,考虑一个INSERT...SELECT并避免循环的INSERT...VALUES In fact, even consider the ANSI version (ie, compliant in all SQL-supported DBMS's) of avoiding duplicates using the NOT EXISTS clause. 实际上,甚至考虑使用NOT EXISTS子句避免重复的ANSI版本(即,在所有SQL支持的DBMS中都兼容)。 But this will only run one action and not conditionally two actions. 但这只会执行一个动作,而不会有条件地执行两个动作。

Below can be run anytime and only unique pairings of date_of_report and extension will be appended, ignoring matches. 下面可以随时运行,只有date_of_report扩展的独特的配对将被追加,忽略了比赛。 Be sure to replace date_of_report and my_report_date_column for actual columns as you never explicitly do so in aggregate query. 请确保将date_of_reportmy_report_date_column替换为实际列,因为您从未在聚合查询中明确地这样做。

INSERT into daily_call_totals
        (date_of_report,                                  -- UPDATE COLUMN
         extension,
         total_talk_time_seconds,
         total_talk_time_minutes,
         total_outbound)
SELECT
      my_report_date_column,                              -- UPDATE COLUMN
      u.extension
      , sum(duration) as total_talk_time_seconds
      , round(sum(duration) / 60,2) as total_talk_time_minutes
      , sum(case when legtype1 = 1 then 1 else 0 end) as total_outbound

FROM SESSION a
JOIN call_summary b
  ON a.notablecallid = b.notablecallid
JOIN system_USERS u
  ON u.EXTENSION = CALLINGPARTYNO or u.EXTENSION = FINALLYCALLEDPARTYNO
WHERE NOT EXISTS
   (SELECT sub.date_of_report, sub.extension
    FROM daily_call_totals sub
    WHERE sub.date_of_report = my_report_date_column     -- UPDATE COLUMN
      AND sub.extension = u.extension)
GROUP BY my_report_date_column, extension, u.user_id     -- UPDATE COLUMN 

Now if you want to run two actions conditionally: 1) update existing values OR 2) insert new values, then use DB2's MERGE (available in some but not all RDBMS's): 现在,如果要有条件地运行两个操作:1)更新现有值或2)插入新值,然后使用DB2的MERGE (在某些但不是全部的RDBMS中可用):

MERGE INTO daily_call_totals AS d
USING
   (SELECT
          my_report_date_column,                              -- UPDATE COLUMN
          u.extension
          , sum(duration) as total_talk_time_seconds
          , round(sum(duration) / 60,2) as total_talk_time_minutes
          , sum(case when legtype1 = 1 then 1 else 0 end) as total_outbound

    FROM SESSION a
    JOIN call_summary b
      ON a.notablecallid = b.notablecallid
    JOIN system_USERS u
      ON u.EXTENSION = CALLINGPARTYNO or u.EXTENSION = FINALLYCALLEDPARTYNO
    GROUP BY my_report_date_column, extension, u.user_id     -- UPDATE COLUMN
   ) AS q
ON (d.date_of_report = q.my_report_date_column               -- UPDATE COLUMN
    AND d.extension = q.extension)
WHEN MATCHED THEN
     UPDATE SET d.total_talk_time_seconds = q.total_talk_time_seconds,
                d.total_talk_time_minutes = q.total_talk_time_minutes,
                d.total_outbound = q.total_outbound            
WHEN NOT MATCHED THEN
     INSERT (date_of_report,                                 -- UPDATE COLUMN
             extension,
             total_talk_time_seconds,
             total_talk_time_minutes,
             total_outbound)
     VALUES (q.my_report_date_column,                        -- UPDATE COLUMN
             q.extension
             q.total_talk_time_seconds
             q.total_talk_time_minutes
             q.total_outbound);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM