4

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

我可以使用以下方法执行代码:

connection.execute"insert into blah blah blah"

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

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

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

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

到目前为止我的代码:

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

但是我一辈子都无法获得执行和阅读结果的程序。

任何人都可以帮忙吗?

谢谢

4

2 回答 2

7

adCmdText如果要执行存储过程,则用于 SQL 查询,那么您必须使用adCmdStoredProc(value 4)

编辑:

'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
于 2013-10-31T12:21:11.470 回答
0

连接。执行“SET NOCOUNT ON”

SET cmd.ActiveConnection = 连接

我过得很好。

于 2021-05-17T12:45:44.827 回答