如何使用ADO从查询中检索所有错误和消息

时间:2010-06-10 10:13:57

标签: sql-server vbscript ado

当SQL批处理从多个消息中返回多个消息时print语句,然后我只能使用ADO连接的Errors集合检索第一个。我如何获得其余的消息?

如果我运行此脚本:

Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLOLEDB"
conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=Master"
conn.Open

conn.Execute("print 'Foo'" & vbCrLf & "print 'Bar'" & vbCrLf & "raiserror ('xyz', 10, 127)")

Dim error
For Each error in conn.Errors
    MsgBox error.Description
Next

然后我只回到“Foo”,从不“Bar”或“xyz”。

有没有办法获取剩余的消息?

1 个答案:

答案 0 :(得分:6)

我自己想出来了。

这有效:

Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLOLEDB"
conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=Master"
conn.Open

Dim rs
Set rs = conn.Execute("print 'Foo'" & vbCrLf & "print 'Bar'" & vbCrLf & "raiserror ('xyz', 10, 127)")

Dim error
While not (rs is nothing)
    For Each error in conn.Errors
        MsgBox error.Description
    Next
    Set rs = rs.NextRecordSet
Wend