这是我在挖掘Sql Server进行三次参数化查询批处理时发现的;
似乎由于@Person_Name
长度变化,Sql查询引擎正在为同一查询创建新的执行计划。我通过循环生成参数化查询,而不是指定SqlDbType Length。
我想要的是使用相同执行计划的所有三个查询,除了值之外,所有内容都应该相同。
这是VB.Net代码。查询。
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