Php 试图破译合并命令相关代码的新手

Php 试图破译合并命令相关代码的新手,php,sql,sql-server,merge,Php,Sql,Sql Server,Merge,我们组中有人退休了,我正试图弄清楚他的merge语句(和相关代码)的作用,以便在发送之前确定如何将一些(不是全部)值转换为整数。有关问题,请参见下面的评论。我是微软SQL的新手,几年前参加过php课程,但没有太多经验。我曾尝试用谷歌搜索merge命令,但其中有几个部分遇到了问题。见下面我的问题。(// ?) 我看过: http://php.net/manual/en/pdo.query.php http://stackoverflow.com/questions/4336573/merg

我们组中有人退休了,我正试图弄清楚他的merge语句(和相关代码)的作用,以便在发送之前确定如何将一些(不是全部)值转换为整数。有关问题,请参见下面的评论。我是微软SQL的新手,几年前参加过php课程,但没有太多经验。我曾尝试用谷歌搜索merge命令,但其中有几个部分遇到了问题。见下面我的问题。(// ?) 我看过:

  http://php.net/manual/en/pdo.query.php
  http://stackoverflow.com/questions/4336573/merge-to-target-columns-using-source-rows
  http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Fsqlp%2Frbafymerge.htm
我意识到这些都是基本问题,但我正试图弄清楚,这里没有人知道

function storeData ($form)
{
global $ms_conn, $QEDnamespace;
//I'm not sure what this is doing??  I thought this was where it was sending data up??
$qry = "MERGE INTO visEData AS Target
    USING (VALUES (?,?,?,?,?,?,?,?,?,?))
           AS Source (TestGUID,pqID, TestUnitID, TestUnitCountID,
           ColorID, MeasurementID, ParameterValue,
           Comments, EvaluatorID, EvaluationDate)
    ON Target.pqID = Source.pqID 
        AND Target.MeasurementID=Source.MeasurementID  //what is this doing? 
        AND Target.ColorID=Source.ColorID  //what is target and source? 
    WHEN MATCHED THEN
        UPDATE SET ParameterValue = Source.ParameterValue,
            EvaluatorID = Source.EvaluatorID, //where is evaluatorID and source? My table or table we're send it to?
            EvaluationDate = Source.EvaluationDate,
            Comments = Source.Comments
    WHEN NOT MATCHED BY TARGET THEN
        INSERT (TestGUID,
            pqID, TestUnitID, TestUnitCountID,
            ColorID, MeasurementID,
            ParameterValue, Comments,
            EvaluatorID, EvaluationDate, TestIndex, TestNumber) 
        VALUES (Source.TestGUID, Source.pqID, 
                       Source.TestUnitID, 
                       Source.TestUnitCountID,
           Source.ColorID, Source.MeasurementID, Source.ParameterValue,
           Source.Comments, Source.EvaluatorID, Source.EvaluationDate,?,?);";

$pqID = coverSheetData($form);
$tid = getBaseTest($form['TextField6']);
$testGUID = getTestGUID($tid);
$testIndex = getTestIndex ($testGUID);
foreach ($form['visE']['parameters'] as $parameter=>$element)
{
    foreach ($element as $key=>$data)
    {
        if ( mb_ereg_match('.+evaluation', $key) === true )
        {
            $testUnitData = getTestUnitData ($form, $key, $tid, $testGUID);
            try
            {
               //I'm not sure if this is where it's sent up?? 
               //Maybe I could add the integer conversion here??
               $ms_conn->query ($qry, array(
                 $testGUID, $pqID,
                 $testUnitData[0], $testUnitData[1], $testUnitData[2],$element['parameterID'], $data, $element['comments']  $QEDnamespace->userid, date ('Y-m-d'), $testIndex, $tid));
                                     }
            catch (Zend_Db_Statement_Sqlsrv_Exception $e)
            {
                dataLog($e->getMessage());
                returnStatus ("Failed at: " . $key);
            }
        }
    }
}

}

这是一个有点长的评论。如果您使用的是SQL Server,请查看上的SQL Server文档。所有SQL Server文档都是在线的,通过Google很容易找到(使用Bing可能更容易)

MERGE命令的目的是一步完成插入和更新。基本上,您有一个包含新数据的表(“源”)和一个要更新的表(“目标”)。当记录匹配时,则使用源中的匹配记录更新目标中的现有记录。当记录不匹配时,将其插入目标

与两个语句相比,MERGE的主要优点不一定是优雅且直观的语法。主要优点是所有操作都发生在一个事务中,因此它们要么作为一个整体成功,要么作为一个整体失败


语法实际上并没有那么糟糕。我建议您建立一个测试数据库,自己尝试几个示例,这样您至少可以理解语法。然后,返回此代码。执行此操作时,打印出生成的merge语句并将其放入SQLServerManagementStudio,在那里您将为该语句提供漂亮的彩色编码关键字。然后一步一步地看一遍,你可能会发现这很有道理。

我想他“退休”是有原因的,你确定他没有因为那个代码而被解雇吗?不,他退休了。我认为它更复杂,因为其他历史数据库不允许您命名项目,另外还有一个多维数据集结构,使用DB的人必须有简单的表,这些表有单个id链接到其他表。我不知道细节。