知道我的查询有什么问题吗?
我正在尝试更新我的数据库的一行,但我使用的查询会导致异常。
UPDATE Table1
SET CHANGE_DELIVERY_REASON = 'Card Activation',
CHANGE_DELIVERY_NOTE = 'ACTIVATION SUCCESS [1 OK - 2 OK]',
RECONCILIATION_STATUS = 1 ,
RECONCILIATION_LAST_UPDATED = '23/4/2018 12:00:00 AM',
RECONCILIATION_COUNT = '6',
ACTIVATION_FAILURE = 'SUCCESS';
我收到此错误:
“SQL错误:ORA-01843:不是有效月份”
我不知道我的查询有什么问题。
updateQuery = " UPDATE " + clsUtility.GetMasterTable() + " SET ";
updateQuery += " CHANGE_DELIVERY_REASON = 'Card Activation', ";
updateQuery += " CHANGE_DELIVERY_NOTE = '" + notes + "', ";
updateQuery += " RECONCILIATION_STATUS = 1 , ";
updateQuery += " RECONCILIATION_LAST_UPDATED = '" + DateTime.Today + "', ";
updateQuery += " RECONCILIATION_COUNT = '" + reconciliation_count + "',";
updateQuery += " ACTIVATION_FAILURE = '" + resultPart + "', ";
答案 0 :(得分:2)
您的问题是您提供的日期的格式,这是在其他答案中处理的。你需要这个用于其他日期字段,所以一定要看看它。
但是,对于这种特殊情况,您可以直接停止使用本地日期并让数据库完成工作:
RECONCILIATION_LAST_UPDATED = trunc(sysdate)
旁注:请在查询中使用参数绑定。您对SQL injection攻击持开放态度。
使用参数绑定,您不仅可以避免SQL注入攻击,还不必担心实际上不需要格式化存储的数据的格式字符串:
using (var connection = new SqlConnection())
{
using (var command = connection.CreateCommand())
{
command.CommandText = "UPDATE " + clsUtility.GetMasterTable() + " SET RECONCILIATION_LAST_UPDATED = :last_updated, CHANGE_DELIVERY_NOTE = :delivery_note";
var parameterLastUpdated = command.CreateParameter();
parameterLastUpdated.ParameterName = ":last_updated";
parameterLastUpdated.SqlDbType = SqlDbType.DateTime;
parameterLastUpdated.Direction = ParameterDirection.Input;
parameterLastUpdated.Value = DateTime.Today;
command.Parameters.Add(parameterLastUpdated);
var parameterDelivery = command.CreateParameter();
parameterDelivery.ParameterName = ":delivery_note";
parameterDelivery.SqlDbType = SqlDbType.NVarChar;
parameterDelivery.Direction = ParameterDirection.Input;
parameterDelivery.Value = "Some string you want in notes";
command.Parameters.Add(parameterDelivery);
command.ExecuteNonQuery();
}
}
答案 1 :(得分:0)
问题是您的日期格式
updateQuery = " UPDATE " + clsUtility.GetMasterTable() + " SET ";
updateQuery += " CHANGE_DELIVERY_REASON = 'Card Activation', ";
updateQuery += " CHANGE_DELIVERY_NOTE = '" + notes + "', ";
updateQuery += " RECONCILIATION_STATUS = 1 , ";
updateQuery += " RECONCILIATION_LAST_UPDATED = TO_DATE('" + DateTime.Today + "','DD/MM/YYYY HH:MI:SS AM'), ";
updateQuery += " RECONCILIATION_COUNT = '" + reconciliation_count + "',";
updateQuery += " ACTIVATION_FAILURE = '" + resultPart + "', ";
尝试这个
答案 2 :(得分:0)
您应该使用TO_DATE()
功能。
将您的一行更改为此行:
updateQuery += " RECONCILIATION_LAST_UPDATED = TO_DATE('" +
DateTime.Today + "', 'DD/MM/YYYY HH:MI:SS AM'), ";
更正后的代码:
updateQuery = " UPDATE " + clsUtility.GetMasterTable() + " SET ";
updateQuery += " CHANGE_DELIVERY_REASON = 'Card Activation', ";
updateQuery += " CHANGE_DELIVERY_NOTE = '" + notes + "', ";
updateQuery += " RECONCILIATION_STATUS = 1 , ";
updateQuery += " RECONCILIATION_LAST_UPDATED = TO_DATE('" + DateTime.Today + "', 'DD/MM/YYYY HH:MI:SS AM'), "; //This Line is changed
updateQuery += " RECONCILIATION_COUNT = '" + reconciliation_count + "',";
updateQuery += " ACTIVATION_FAILURE = '" + resultPart + "', ";
常规语法:
Oracle / PLSQL中TO_DATE函数的语法是:
TO_DATE( string1 [, format_mask] [, nls_language] )
有关详细信息,请访问以下链接: