我有一个准备好的语句,如果行以前不存在,则该语句旨在插入行。如果只做一行,它工作正常。如果有两个,则添加第一行,忽略第二行。如果第一行存在而第二行不存在,则添加第二行并且所有后续行都失败。
实际上,插入工作一次,就好像 IF NOT EXISTS 不会更新新参数的绑定。
这是示例代码:
$dbConn = 'mssql:host=' . $server . ';dbname=' . $base;
$dbh = new PDO( $dbConn, $user, $pass);
// Go through each course code and add it
// Ignore if it already exists
$stmt = $dbh->prepare('IF NOT EXISTS (SELECT * FROM _ExamCoursesTEMP
WHERE ExamCourseCode = :examCode AND CourseCode = :courseCode )
BEGIN
INSERT INTO _ExamCoursesTEMP ( ExamCourseCode, CourseCode ) VALUES ( :examCode2, :courseCode2 )
END');
$counter = 0;
foreach( $courseCodes as $courseCode )
{
$stmt->bindParam(':examCode', $examCode );
$stmt->bindParam(':courseCode', $courseCode );
$stmt->bindParam(':examCode2', $examCode );
$stmt->bindParam(':courseCode2', $courseCode );
$updateCount = $stmt->execute();
$counter++;
}
第一个 updateCount 返回 1,其余的都是空的。
我不确定这是否是我的代码、通过 PDO 或 mssql 的怪癖准备的语句绑定的问题。
任何帮助,将不胜感激。
谢谢。