我正在将一个项目从MySQL迁移到MS SQL。这与我以前的习惯有很大不同,因为我是从1个MySQL表转为MS SQL视图和表。我希望可以坚持使用MySQL,但这是我无法控制的。
我需要插入数据,否则将数据更新到sql表中。这是我的尝试和错误。请帮助:
$serverName = "myserver";
$connectionInfo = array( "Database"=>"sqldatabasename", "UID"=>"myusername", "PWD"=>"youguysarethebest" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "INSERT INTO CallSettings (Order_Number, ODetail_id, Serial_Id, AssignedPerson, Status, Comments, Customer_IPAddress, customer_assistance_name, customer_assistance_number, customer_assistance_email,networksettings, addressbook, accountcodes, installedprintdrivers) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ELSE UPDATE CallSettings SET (Order_Number= (?), ODetail_id= (?), Serial_Id= (?), AssignedPerson= (?), Status= (?), Comments= (?), Customer_IPAddress= (?), customer_assistance_name= (?), customer_assistance_number=(?), customer_assistance_email= (?), networksettings= (?), addressbook= (?), accountcodes= (?), installedprintdrivers= (?))";
$params = array($Order_Number, $ODetail_id, $Serial_Id, $AssignedPerson, $Status, $Comments, $Customer_IPAddress, $customer_assistance_name, $customer_assistance_number, $customer_assistance_email, $networksettings, $addressbook, $accountcodes, $installedprintdrivers);
$stmt = sqlsrv_query( $conn, $sql, $params);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close($conn);
错误
Array ( [0] => Array ( [0] => 07002 [SQLSTATE] => 07002 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error [message] => [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error ) )
答案 0 :(得分:0)
错误消息是因为传递的参数比预期的要少(14个而不是28个)。
您需要查询中所有?
标记的参数。您仅传递前14个元素的参数(更新部分还有14个参数,即使它们与第一部分相同),因此您需要在查询调用中重复参数数组(参数列表的2倍) )以传递所有值。
$stmt = sqlsrv_query( $conn, $sql, array_merge($params,$params));