1

I am getting error while executing below query in VB (I am using macro to run this query). But in ORACLE, the same query runs file and gives me the expected result. Please note that, "XML_FIELD" is a CLOB Which has XML Data in it. I am trying to extract data from "XML_FIELD" using VB Macro.

Query in SQL: executes fine :
---> select xmltype(XML_FIELD).extract('//XML_TAGNAME/text()').getStringVal() as NEW_COL from TAB_1 where TEST_ID = 123

Error in VB: SQL Execution Error ora-00936 missing expression

Query in VBA:
Sub Extract_CLOB()
Dim SQL As String
Dim OraDynaSet As Object
'Comment: I am using two single quotes (' ' ) to escape the other single quote
SQL = "select xmltype(XML_FIELD).extract(''//XML_TAGNAME/text()'').getStringVal() as NEW_COL from TAB_1 where TEST_ID = 123"
Set OraDynaSet = objDataBase.DBCreateDynaset(SQL, 0&)
End Sub

1
  • Go into debug mode, print out the value of SQL and post it. Commented Nov 30, 2012 at 6:05

1 Answer 1

0

I tried this code and it worked well. I added one clob field to emp table.

Public objSession As Object
Public objDataBase As Object

Sub ConnectToOracle()
    'Create a reference to the OO4O dll
    Set objSession = CreateObject("OracleInProcServer.XOraSession")
    'Create a reference to my database
    Set objDataBase = objSession.OpenDatabase("mydb", "scott/tiger", 0&)
    Dim strSQL As String
Dim strTxt As String
Dim OraDynaSet As Object

strSQL = "select xmltype(MyXMLField).extract('//ROWSET//ROW//JOB/text()').getStringVal() as NEW_COL from emp where empno = 2"

'Retrieve the results from Oracle
Set OraDynaSet = objDataBase.DBCreateDynaset(strSQL, 0&)

strTxt = CStr(OraDynaSet.Fields(0).Value)

MsgBox strTxt
End Sub

and my xml is:

<?xml version="1.0"?>
      <ROWSET>
         <ROW num="1">
            <EMPNO>7</EMPNO>
            <ENAME>S</ENAME>
            <JOB>CLERK</JOB>
            <MGR>7</MGR>
            <HIREDATE>12/17/2008 0:0:0</HIREDATE>
           <SAL>999</SAL>
           <DEPTNO>20</DEPTNO>
        </ROW>
     </ROWSET>
Sign up to request clarification or add additional context in comments.

6 Comments

I have used two single quotes to escape the other. extract(''//XML_TAGNAME/text()'') - here I have used two single quotes (' ')
How are you connecting to Oracle? Are you using Oracle Objects for OLE or through ODBC? Have you tried to run other SQL statements?
Below is the code to connect to DB. I get Connection Successful message. Other queries works fine. <br/> Private Sub StartConnection() 'Create a Session Object <br/> Set objSession = CreateObject("OracleInProcServer.XOraSession") <br/> dbName = .Cells(4, 3).Value <br/> dbUserName = .Cells(5, 3).Value <br/> dbPwd = .Cells(6, 3).Value <br/> Set objDataBase = objSession.OpenDatabase(dbName, dbUserName & "/" & dbPwd, 0&) <br/> MsgBox "Connection Successful" <br/> 'Set objDataBase = objSession.OpenDatabase("QTCSTG", "APPSRO/read0nly", 0&) <br/> Call Extract_CLOB <br/> End Sub <br/>
Sorry for not formatting the above comment. Tried giving 2 spaces to insert line break, and tried with <br/>, still all the lines are displaying inline.
Please, post here your SQL variable's value using debug mode as suggested by ElectricLlama.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.