我正在执行从Excel中的工作表连接的sql语句,并将结果返回到另一个工作表。如果我运行一个内联嵌套查询,结果会返回没有错误(虽然性能慢了3分钟对几秒)如果我使用公共表表达式CTE,如下所示我得到运行时错误' 3704'关闭对象时不允许操作。我正在使用Excel 2010并查询Oracle 11g数据库。查询在Toad中正常工作,我似乎记得在Excel中针对Oracle运行CTE查询,所以我不认为这是问题所在。我无法控制对DB进行任何更改或将其设置为存储过程。提前感谢您的任何帮助。
完整代码为保密而清理:
Sub RefreshData()
'Turn off screen updating and alerts
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'User cell selection when running macro
Dim iRowRef As Integer
Dim iColRef As Integer
SheetRef = ActiveSheet.Name
iRowRef = ActiveCell.Row
iColRef = ActiveCell.Column
Worksheets("Dashboard").Cells(2, 2).Value = "Start time: " & Now()
'Create connection
Dim cn As Object
Dim rs As Object
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
If strUname = "" Then
strUname = InputBox(Prompt:="USERNAME", Title:="Authentication", Default:=(Environ$("Username")))
End If
If strPword = "" Then
'strPword = InputBox(Prompt:="PASSWORD", Title:="Authentication", Default:="Password")
strPword = modPWMask.InputBoxPW("PASSWORD", "Authentication")
End If
***编辑:问题与连接字符串中的提供程序有关。 CTE支持取决于提供者。谢谢@TimWilliams
'Connection string
cn.ConnectionString = "Provider=MSDAORA.1;User ID=" & strUname & ";Password=" & strPword & ";Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = host.com)(PORT = 100))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = db_prd)))"
'Open connection
cn.Open
'Query Data Runoff
strQuery = Worksheets("SQL").Cells(2, 2).Value & UCase(Format(Worksheets("Dashboard").Cells(7, 3).Value, "dd-mmm-yyyy")) & Worksheets("SQL").Cells(2, 3).Value
'Send query string to clipboard
Dim DataObj3 As New MSForms.DataObject
DataObj3.SetText strQuery
DataObj3.PutInClipboard
'Clear old data
Worksheets("Data").Cells.EntireColumn.Hidden = False
Worksheets("Data").Range("C20:E20").ClearContents
'Create recordset from query
Set rs = cn.Execute(strQuery)
这是我收到错误的行:
'Paste data from record set
Worksheets("Data").Range("C20").CopyFromRecordset rs
'Timestamp end time
Worksheets("Dashboard").Cells(3, 2).Value = "Last refreshed: " & Now()
'Turn on screen updating and alerts
Application.ScreenUpdating = True
Application.DisplayAlerts = True
'Return to starting cell
Worksheets(SheetRef).Activate
Cells(iRowRef, iColRef).Select
End Sub
这是SQL再次因机密性而被清理:
with cte as
(
select
ENTITY
, ENDPOINT
, case
when RWS = 1 and ENDPOINT in (13,14) then PCT_B
when RWS = 2 and ENDPOINT in (13,14) then PCT_A
else PCT_A end as PCT
, SUM(case
when RWS = 1 and ENDPOINT in (13,14) then cast(BAL_B AS decimal (18,2))
when RWS = 2 and ENDPOINT in (13,14) then cast(BAL_A AS decimal (18,2))
when RWS = 2 then cast(BAL AS decimal (18,2))
else 0 end) as BAL
, ROUND(SUM((case
when RWS = 1 and ENDPOINT in (13,14) then cast(BAL_B AS decimal (18,2))
when RWS = 2 and ENDPOINT in (13,14) then cast(BAL_A AS decimal (18,2))
when RWS = 2 then cast(BAL AS decimal (18,2))
else 0 end)
*
(case
when RWS = 1 and ENDPOINT in (13,14) then PCT_B
when RWS = 2 and ENDPOINT in (13,14) then PCT_A
else PCT_A end)
/100),2) as ADJ_BAL
from
SCHEMA.VIEW
cross join (select 1 as RWS from dual union select 2 as RWS from dual)
where
AS_OF_DATE = '01-NOV-2016'
group by
ENTITY
, ENDPOINT
, case
when RWS = 1 and ENDPOINT in (13,14) then PCT_B
when RWS = 2 and ENDPOINT in (13,14) then PCT_A
else PCT_A end
)
select sum(case
when ENTITY = 'ENTITYA' and ENDPOINT <> 33 then ADJ_BAL
when ENTITY = 'ENTITYB' then ADJ_BAL
when ENTITY = 'ENTITYC' and ENDPOINT <> 33 then ADJ_BAL
else 0
end) as BAL_CONSOL
, sum(case
when ENTITY = 'ENTITYA' then ADJ_BAL
else 0
end) as BAL_ENTITYA
, sum(case
when ENTITY = 'ENTITYB' then ADJ_BAL
else 0
end) as BAL_ENTITYB
from cte
答案 0 :(得分:1)
适用于提供商OraOLEDB.Oracle
和
cn.CursorLocation = adUseClient
无法让它与MSDAORA.1
提供商