在ASP中,如何从参数化查询中返回多个记录集?

时间:2014-04-16 16:44:59

标签: sql asp-classic adodb

我可以找到这个问题的每一部分,但它们不能一起工作。将sql字符串传递给Recordset.Open时,多个记录集工作正常,但是一旦升级到ADODB.Command,Command.Execute和Recordset.Open都不会从提供的命令返回多于第一个记录集。相反,我无法在不依赖Command的情况下使用参数化查询。

以下是我对SQL代码的全部内容:

declare @var int
select @var = column from table where othercolumn = ?
(other stuff with @var)

声明似乎是一个记录集,所以我无法获得任何信息。我对将它放在存储过程中犹豫不决,因为它有助于扩散,但如果这是我做的唯一方法。

1 个答案:

答案 0 :(得分:1)

这是一个基本的例子,魔术线是;

Set rs = rs.NextRecordSet

如果存在拼写错误,则伪编码如此道歉。

Dim cmd, rs, sql, connstring

Set cmd = Server.CreateObject("ADODB.Command")

connstring = "your connection string here"

sql = ""
sql = sql & "SELECT col1, col2 FROM table1 WHERE col0 = ?" & vbCrLf
sql = sql & "SELECT col1, col2 FROM table2 WHERE col0 = ?"

With cmd
  .ActiveConnection = connstring
  .CommandType = adCmdText
  .CommandText = sql
  .Parameters.Append(.CreateParameter("@table1id", adInteger, adParamInput, 4))
  .Parameters.Append(.CreateParameter("@table2id", adInteger, adParamInput, 4))
  'Return first Recordset for table1
  'id1 and id2 contain your values to pass in your parameters.
  Set rs = .Execute(, Array(id1, id2))
  'Do something with your data, use .GetRows() to return an array or something
  'Then return your second Recordset for table2
  Set rs = rs.NextRecordSet
  'Do something with your data, use .GetRows() to return an array or something
  Call rs.Close()
  Set rs = Nothing
End With
'Bit of cleanup
Set cmd = Nothing