所以这真的很奇怪。奇怪的SQL错误(Bug)
我在sqlserver上运行一个带有'Select Count(*)'的.net命令,得到一个类似于“需要注意CA”的响应(这是在内部连接表的一个记录的一个字段的varchar中)。
咦? Count(*)如何返回一个字符串?该代码正确执行1000次中的999次。有时候,在某些客户端服务器上,它会在一个小时左右的时间内发出一串错误,以便再次奇迹般地停下来。
这是我的SqlCommand:
SELECT Count(*)
FROM patientsappointments
INNER JOIN appointmenttypes
ON patientsappointments.appointmenttypeid =
appointmenttypes.appointmenttypeid
WHERE ((patientsappointments.date > @WeekStartDate
AND patientsappointments.date < @WeekFinishDate)
AND (patientsappointments.status = 'Pending')
AND (patientsappointments.doctorid = @DoctorID)
AND (appointmenttypes.appointmentname <> 'Note'))
而这些参数:
@WeekStartDate = 24/06/2013 12:00:00 AM (DateTime)
@WeekFinishDate = 1/07/2013 12:00:00 AM (DateTime)
@DoctorID = 53630c67-3a5a-406f-901c-dbf6b6d1b20f (UniqueIdentifier)
我做了sqlcmd.executescalar得到结果。有任何想法吗?
实际执行的代码是:
SyncLock lockRefresh
Dim WeekFulfilled, WeekPending As Integer
Using conSLDB As New SqlConnection(modLocalSettings.conSLDBConnectionString)
Dim mySQL As SqlCommand
mySQL = New SqlCommand("SELECT COUNT(*) FROM PatientsAppointments INNER JOIN AppointmentTypes ON PatientsAppointments.AppointmentTypeID = AppointmentTypes.AppointmentTypeID " & _
"WHERE ((PatientsAppointments.Date > @WeekStartDate AND PatientsAppointments.Date < @WeekFinishDate) AND (PatientsAppointments.Status = 'Pending') " & _
"AND (PatientsAppointments.DoctorID = @DoctorID) AND (AppointmentTypes.AppointmentName <> 'Note'))", conSLDB)
Try
mySQL.Parameters.Add("@WeekStartDate", SqlDbType.DateTime).Value = MonthCalendar1.SelectionStart.Date.AddDays(-MonthCalendar1.SelectionStart.Date.DayOfWeek).AddDays(1)
mySQL.Parameters.Add("@WeekFinishDate", SqlDbType.DateTime).Value = MonthCalendar1.SelectionStart.Date.AddDays(-MonthCalendar1.SelectionStart.Date.DayOfWeek).AddDays(8)
mySQL.Parameters.Add("@DoctorID", SqlDbType.UniqueIdentifier).Value = cboDoctors.SelectedValue
conSLDB.Open()
'got errors here like "Conversion from string "R2/3" to type 'Integer' is not valid." Weird.
'failing on deadlock - maybe due to simultaneous updating from udp event. Try adding random delay to refresh
WeekPending = mySQL.ExecuteScalar
Catch ex As Exception
ErrorSender.SendError("frmAppointmentBook - RefreshHeader 1", ex, New String() {String.Format("mySQL.commandtext: {0}", mySQL.CommandText), _
String.Format("mySQL.Parameters: {0}", clsErrorSender.ParamsListToString(mySQL.Parameters))})
End Try
Me.lblPendingWeek.Text = WeekPending
Try
mySQL.CommandText = "SELECT COUNT(*) FROM PatientsAppointments INNER JOIN AppointmentTypes ON PatientsAppointments.AppointmentTypeID = AppointmentTypes.AppointmentTypeID WHERE " & _
"(PatientsAppointments.Date > @WeekStartDate AND PatientsAppointments.Date < @WeekFinishDate) AND (PatientsAppointments.Status = 'Fulfilled') AND " & _
"(PatientsAppointments.DoctorID = @DoctorID) AND (AppointmentTypes.AppointmentName <> 'Note')"
'didn't get the error here... but just in case...
WeekFulfilled = mySQL.ExecuteScalar
Catch ex As Exception
ErrorSender.SendError("frmAppointmentBook - RefreshHeader 2", ex, New String() {String.Format("mySQL.commandtext: {0}", mySQL.CommandText)})
End Try
conSLDB.Close()
End Using
End SyncLock
确切的错误信息是:
System.InvalidCastException
Conversion from string "Needs Attention DC" to type 'Integer' is not valid.
.NET应用程序正在执行不同于预期的'SQL'部分。或者在你的程序/函数中,另一个'SELECT'语句在'COUNT'之后执行。在管理工作室运行声明,你会得到什么? –
什么是**确切**错误信息? ''需要注意CA“'不是SQL Server错误消息。 –
@MartinSmith - 我不认为有错误信息。 SQL正在返回不同于预期的输出。 –