所以這真的很奇怪。奇怪的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正在返回不同於預期的輸出。 –