Hello.I just want to first point out that I do not like the procedure that we have in place, but my protests have not helped at all.We currently monitor change tracking by inserting the new record into a global temporary table. Then a SP is run to copy the record from the base table into a changes table. Then the base table is updated.We do not get the benefit of a prepared statement by using an insert statement nor can I see any use of the temporary table except to consume resources and slow the process down. The permissions are the same for all tables so that point is moot. If anyone knows any other reasons why this procedure is poor please state them so I can use them in my next argument.The problem is that the temporary table disappears once in a while before the insert. Most of the time it works.I shortened the field list for clarity.Here is the code (imsDataServices is a reference to a third party data manager that I steal the connection from): If CreateTempTableRI_Note(cmd) Then If InsertIntoTempTableRI_Note(cmd, SaveType, RI_Note) Then 'cmd = New SqlCommand(gconSPRotair_RI_Note, New imsDataServices().Connection) cmd.CommandText = gconSPRotair_RI_Note cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.Clear() Parameter = cmd.Parameters.Add("@ReturnMessage", SqlDbType.VarChar, 100) Parameter.Direction = ParameterDirection.Output Parameter = cmd.Parameters.Add("@ReturnCode", SqlDbType.Int) Parameter.Direction = ParameterDirection.Output mintSPRC = cmd.ExecuteNonQuery If mintSPRC = -1 Then If Val(cmd.Parameters(1).Value) = 0 Then Return True Else Return False End If Else mstrLastError = cmd.Parameters(0).Value.ToString Return False End If End If End If
Here is CreateTempTableRI_Note: cmd = New SqlCommand("SET NOCOUNT ON", New imsDataServices().Connection) 'cmd = New SqlCommand 'cmd.Connection = New imsDataServices().Connection If Not TempTableExists(cmd.Connection, gconTempTableRI_Note) Then intRC = 0 strSQL = "SELECT " & _ [String].Format("{0}", _ gcontblRI_Note_DeleteBy) & _ " INTO " & gconTempTableRI_Note & _ " FROM " & gconTableRI_Note & _ " WHERE 0 = 1" Else intRC = -1 strSQL = "TRUNCATE TABLE " & gconTempTableRI_Note End If cmd.CommandText = strSQL mintSPRC = cmd.ExecuteNonQuery If mintSPRC = intRC Then Return True Else Return False End If
Here is InsertIntoTempTableRI_Note: strSQL = "INSERT INTO " & gconTempTableRI_Note & _ " (" & [String].Format("{0}", _ gcontblRI_Note_DeleteBy,) & ")" & _ " VALUES (" & gconParameter_Note_DeleteBy & ")" cmd.CommandText = strSQL With RI_Note cmd.Parameters.AddWithValue(gconParameter_Note_DeleteBy, IIf(.Field("DeleteBy") = vbNullString, DBNull.Value, .Field("DeleteBy"))) End With If cmd.ExecuteNonQuery > 0 Then Return True Else Return False End If
Any assistance is appreciated.