This is what I found when digging into Sql Server for three executions of a parameterized query batch;
It seems that because of the @Person_Name
length variation, Sql query engine is creating new execution plans for the same query. I am generating a parameterized query through a loop and not specifying SqlDbType Length.
What I want to have is all three queries using the same execution plan as everything should be identical except the values.
Here is the VB.Net code. For query.
Dim fields_Count As Integer = FieldsList.Split(",").Count
If fields_Count = Values.Count Then
Dim insert_Param As New List(Of FieldsValuesParamList)
insert_Param = get_FieldsValuesParamList(FieldsList, Values)
Dim fields_String As String = String.Join(",", insert_Param.Select(Function(t) t._Field_Name).ToArray())
Dim param_String As String = String.Join(",", insert_Param.Select(Function(t) t._Param_Name).ToArray())
Dim sql_Insert As String
sql_Insert = String.Concat("INSERT INTO ", TableName, " (", fields_String, ") VALUES(", param_String, ")")
sql_Command.CommandText = sql_Insert
For Each paramValue In insert_Param
sql_Command.Parameters.Add(paramValue._Param_Name, SqlDbType.VarChar).Value = paramValue._Value_Name
'sql_Command.Parameters.AddWithValue(paramValue._Param_Name, paramValue._Value_Name)
Next
sql_Command.ExecuteNonQuery()
sql_Command.Parameters.Clear()
Else
Throw New Exception("Fields and Values count should be equal.")
End If