执行存储过程并返回结果集

Rob*_*w84 2 vbscript

我是一个完整的VBScript新手,我正在尝试执行存储过程并读取结果集.我尝试了许多不同的方法在线使用文章,但没有任何作用,我很难过.数据库是SQL Server 2008 R2,该应用程序是一个现成的ERP系统,但我可以添加自己的代码.

我可以使用以下命令执行代码:

connection.execute"insert into blah blah blah"
Run Code Online (Sandbox Code Playgroud)

我可以使用以下方法读取结果集:

Set objRS = CreateObject("ADODB.Recordset")

objRS.Open "select a, b, c FROM blahblah", Connection, adOpenStatic, adLockBatchOptimistic,    adCmdText
If objRS.EOF = False Then
    a = objRS.Fields("a").Value
    b = objRS.Fields("b").Value
    c = objRS.Fields("c").Value
End If
objRS.Close
Run Code Online (Sandbox Code Playgroud)

有问题的存储过程实际上是一个select语句,例如:

create procedure [dbname].[dbo].[sptestproc] 
as 
    @Option1 Varchar(10) = NULL,
    @Option2 Varchar(10) = NULL
AS
BEGIN
    select first, second 
    from table1 
    where a = @option1 and b = @toption2
End
Run Code Online (Sandbox Code Playgroud)

我的代码到目前为止:

Dim sql

sql = "EXEC [dbname].[dbo].[sptestproc] '" & Opt1 & "','" & Opt2 & "'"
Set RS = CreateObject("ADODB.Recordset")
RS.Open sql, Connection, adOpenStatic, adLockBatchOptimistic, adCmdText
Do While Not RS.EOF
    Call App.MessageBox("first",vbInformation,"Data updated")
    Call App.MessageBox("second",vbInformation,"Data updated")
    RS.MoveNext
Loop
Run Code Online (Sandbox Code Playgroud)

但我不能为我的生活得到一个程序来执行和阅读结果.

有人可以帮忙吗?

谢谢

med*_*eda 6

adCmdText如果你想执行存储过程然后你必须使用adCmdStoredProc(值4代替)将用于SQL查询

编辑:

'Set the connection
'...............

'Set the command
DIM cmd
SET cmd = Server.CreateObject("ADODB.Command")
SET cmd.ActiveConnection = Connection

'Set the record set
DIM RS
SET RS = Server.CreateObject("ADODB.recordset")

'Prepare the stored procedure
cmd.CommandText = "[dbo].[sptestproc]"
cmd.CommandType = 4  'adCmdStoredProc

cmd.Parameters("@Option1 ") = Opt1 
cmd.Parameters("@Option2 ") = Opt2 

'Execute the stored procedure
SET RS = cmd.Execute
SET cmd = Nothing

'You can now access the record set
if (not RS.EOF) THEN
    first = RS("first")
    second = RS("second")
end if

'dispose your objects
RS.Close
SET RS = Nothing

Connection.Close
SET Connection = Nothing
Run Code Online (Sandbox Code Playgroud)