以下SQL可以在SQL Server 2008 R2查询工具中运行,但是在Excel中,ADO RS在调用RS.OPEN后关闭... On Error已设置但RS.Open上没有发生错误,但在检查RS上.EOF抛出“记录集已关闭”消息。 Con是开放的,可与其他RS.Open呼叫一起使用。
SQL有点复杂,可能是ADO没有处理它。 SQL构建了Facility_ID的CSV列表。此SQL适用于ADO.Net。
我缺少什么/或者ADO是否无法处理这个多语句SQL?
SQL = "declare @tmp varchar(250);" & vbCrLf _
& " SET @tmp = '';" & vbCrLf _
& " " _
& " SELECT @tmp = @tmp + LTRIM(Facility_ID) + ', '" _
& " FROM Facility_EI" _
& " WHERE Cover_Letter = '" & sCategory & "'" _
& " ORDER BY Facility_ID;" & vbCrLf _
& " " _
& " select SUBSTRING(@tmp, 1, LEN(@tmp)-1);"
RS.Open SQL, con
If RS.EOF Then
MsgBox "Category lookup failed: " & sCategory
GoTo local_exit
End If
sFacIDList = RS(0) ' list of facilities in the category
RS.Close
这是清理过的SQL - 修改第一个SELECT以使用您的表进行测试:
declare @tmp varchar(250);
SET @tmp = ''
SELECT @tmp = @tmp + LTRIM(Facility_ID) + ', '
FROM Facility_EI
WHERE Cover_Letter = 'AT&T BUGs'
ORDER BY Facility_ID
SELECT SUBSTRING(@tmp, 1, LEN(@tmp)-1) CSV_List