Sql 经典ASP-将脚本转换为使用导致问题的参数
我有一个非常大的搜索脚本,它直到现在还没有被参数化,虽然它受到正则表达式函数的半保护,并且不向公众开放,但在刚刚转换它之后,我遇到了一些问题,我使用的方法是获取记录数 这是我的脚本的一个示例,在参数化之前:Sql 经典ASP-将脚本转换为使用导致问题的参数,sql,asp-classic,Sql,Asp Classic,我有一个非常大的搜索脚本,它直到现在还没有被参数化,虽然它受到正则表达式函数的半保护,并且不向公众开放,但在刚刚转换它之后,我遇到了一些问题,我使用的方法是获取记录数 这是我的脚本的一个示例,在参数化之前: selectClause = "SELECT column FROM table " whereClause = "WHERE something = 'something' " If something = "someCondition" Then whereClause = where
selectClause = "SELECT column FROM table "
whereClause = "WHERE something = 'something' "
If something = "someCondition" Then
whereClause = whereClause & "AND something = '"&input&"' "
End If
If somethingElse = "someOtherCondition" Then
whereClause = whereClause & "AND somethingElse = '"&input&"' "
End If
' ... plus loads more conditional statements...
orderClause = "ORDER BY something DESC "
' get the total number of records, which is now what I am stuck on ////
SQL = " SELECT COUNT(*) AS total FROM ("&selectClause & whereClause&") AS Q1; "
Set rs = Conn.Execute(SQL)
If NOT rs.EOF Then
Session("record-count") = rs.Fields("totalRecords")
End If
rs.Close
' calculate paging and limitClause
... some code
limitClause = limit 0, 20 ' example
' build query
SQL = selectClause & whereClause & orderClause & limitClause
Conn.Execute(SQL)
除了容易受到攻击之外,这种方法也很有效。所以我开始使用参数对其进行转换,但现在我不知道如何在不必再次编写整个搜索查询的情况下获得总计数,这是我上面示例长度的30倍。希望下面的代码能更好地解释我的情况
selectClause = "SELECT column FROM table "
whereClause = "WHERE something = 'something' "
If something = "someCondition" Then
whereClause = whereClause & "AND something = ? "
Set newParameter = cmdConn.CreateParameter("@blah1", ad_varChar, ad_ParamInput, Len(input), Replace(input,"'","\'"))
cmdConn.Parameters.Append newParameter
End If
If somethingElse = "someOtherCondition" Then
whereClause = whereClause & "AND somethingElse = ? "
Set newParameter = cmdConn.CreateParameter("@blah2", ad_varChar, ad_ParamInput, Len(input), Replace(input,"'","\'"))
cmdConn.Parameters.Append newParameter
End If
orderClause = "ORDER BY something DESC "
' but now the whereClause contains ?'s which cannot be used here, which has confused me on how I should be getting the new count.
' /////////////////////////////
SQL = " SELECT COUNT(*) AS total FROM ("&selectClause & whereClause&") AS Q1; "
Set rs = Conn.Execute(SQL)
If NOT rs.EOF Then
Session("record-count") = rs.Fields("totalRecords")
End If
rs.Close
' /////////////////////////////
' calculate paging and limitClause
... some code
limitClause = "limit x, y;"
' build query
SQL = selectClause & whereClause & orderClause & limitClause
cmdConn.CommandText = SQL
如您所见,我不能使用以前使用的count方法,因为where子句包含?,这意味着特定的count查询需要自己的参数集合。有谁能建议我现在如何不必重复这个庞大的查询就可以得到我的计数
猜测一下,我可能会创建另一个命令对象cmdConn2,在我设置参数的地方,设置另一个与cmdConn2相同但可以用于计数查询的命令对象,但我的猜测并不总是最好的方法
非常感谢您的帮助。因此,除了select和order by子句外,这两个查询的所有内容基本相同 因此,一个答案是: 将select和order by子句之外的所有内容移动到子例程中。 将select子句设置为select column from table,调用用于公共处理的子例程,然后适当地设置order by子句并执行SQL。 然后将select子句设置为select count*from table,调用用于公共处理的子例程,然后执行SQL no order by子句。 或者,您可以使用类似于第一次查询结果的任何方法来获取返回的记录数吗