Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

vba - Why can't I do a "with x as (...)" with ADODB and Oracle?

I fail to execute an SQL query with a with clause via ADODB and Oracle.

That is, the following snippet works:

Dim cn As ADODB.connection
Set cn = ....

Dim rs As ADODB.recordSet
Set rs = New ADODB.Recordset

rs.Open "select 'foo' x from dual", cn


Do While Not rs.eof
   ...
   rs.MoveNext
Loop

However, the following doesn't work - it genererats a Run-Time error 3704: Operation is not allowed when the object is closed.

Dim cn As ADODB.connection
Set cn = ....

Dim rs As ADODB.recordSet
Set rs = New ADODB.Recordset

rs.Open "with w as (select 'foo' x from dual) select x from w", cn

Do While Not rs.eof
   ...
   rs.MoveNext
Loop

Obviously, this is a trimmed-down demonstration of the real problem which consists of a more sophisticated query.

It seems to me that ADODB sort of parses the query before it passes it to the Oracle instance and does not understand the with clause. Anyway, any help on this is highly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, it really seems as though ADODB expects a query statement to actually start with select. Therefore, a work around for the problem might be to enclose the statement in a select * from ( .... ) like so:

Dim sql As String
sql = "with w as (select 'foo' x from dual) select x from w"

' enclose the statement:
sql = "select * from (" & sql & ")"

rs.Open sql, cn

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...