0

This SQL query works perfectly in my Advanced Query Tool setup, but returns a Syntax Error when I plug it into the following SQL query:


  Dim conn As ADODB.Connection
  Dim rs As ADODB.Recordset
  Dim sConnString As String
  Dim wbNew As Workbook  ' Define a variable for the new workbook

  ' Create the connection string.
  sConnString = "Driver={Oracle in OraClient11g_home1};Dbq=XXXXXX;Uid=XXXXXX;Pwd=XXXXXX;"

  ' Create the Connection and Recordset objects.
  Set conn = New ADODB.Connection
  Set rs = New ADODB.Recordset

  ' Open the connection and execute.
  conn.Open sConnString
  Set rs = conn.Execute("
SELECT W.WONUM, W.STATUS, W.LOCATION, W.WAMSUBWORKTYPE, W.DESCRIPTION, WF.SURVEYDATE, SA.STREETADDRESS, W.WAMTOWN, WF.REGULATORISSUETYPE, WF.RECORDBY, WF.RECORDDATETIME, WF.PIPECONDITION, WF.COMMENTS, L.LOCATION, W.REPORTDATE, WF.RECORDBY, LOC.METERLOCATION, LOC.LKSVYLASTINSPECTIONBY, LOC.LKSVYCOMPLIANCESUBCATEGORY FROM MAXIMO.WORKORDER W LEFT JOIN MAXIMO.LOCATIONS L ON L.LOCATION = W.LOCATION left join (select * from (select location, assetattrid, alnvalue from maximo.locationspec) pivot (max(alnvalue) for assetattrid in ('METERLOCATION' as METERLOCATION, 'LKSVYLASTINSPECTIONBY' as LKSVYLASTINSPECTIONBY, 'PIPECONDITION' as PIPECONDITION, 'LKSVYCOMPLIANCESUBCATEGORY' as LKSVYCOMPLIANCESUBCATEGORY))) LOC ON LOC.LOCATION = W.LOCATION left join (select * from (select wonum, assetlocid, recorddatetime, formname, attributename, attributevalue from maximo.wamformdata where formname='ATMCORROSIONIMS') pivot (max(attributevalue) for attributename in ( 'REGULATORISSUETYPE' AS REGULATORISSUETYPE
, 'SURVEYDATE' as SURVEYDATE,'RECORDBY' as RECORDBY,'PIPECONDITION' as PIPECONDITION,'COMMENTS' as COMMENTS ))) WF on w.location=wf.assetlocid LEFT JOIN maximo.serviceaddress sa ON l.SADDRESSCODE = SA.addresscode WHERE W.WAMTOWN like 'MA-%' AND (WF.PIPECONDITION LIKE ('POOR') OR WF.PIPECONDITION LIKE ('%HAZARD')) AND WF.RECORDDATETIME between TO_DATE('2024-06-15','yyyy-mm-dd') AND TO_DATE('2024-06-25','yyyy-mm-dd') AND W.STATUS IN ('INREVIEW', 'RDISP','INIT','CLOSE') AND W.WAMSUBWORKTYPE IN ('SERV-ATSCR-A-T','SERV-ATMCR-A-T','SERV-ATMCI-A-T','SERV-ATSCI-A-T','SERV-ATMRR-A-T','SERV-ATSRR-A-T','SERV-ATMVR-A-T','SERV-ATSVR-A-T','SERV-MTPRT-A-T','SERV-ATSCP-A-T','SERV-ATMCP-A-T')")



' Check we have data.
  Dim n As Long
  If Not rs.EOF Then
  
    ' Create a new workbook
    Set wbNew = Workbooks.Add
    With wbNew.Sheets(1)
        ' Header
        For n = 1 To rs.Fields.Count
            .Cells(1, n) = rs.Fields(n - 1).Name
        Next
    
        ' Copy data to the new workbook (starting from A2)
        .Range("A2").CopyFromRecordset rs
    End With

  Else
        MsgBox "Error: No records returned.", vbCritical
  End If

  ' Close the recordset and connection
  rs.Close
  If CBool(conn.State And adStateOpen) Then conn.Close

  ' Clean up
  Set conn = Nothing
  Set rs = Nothing
  Set wbNew = Nothing  ' Release the reference to the new workbook

End Sub

I tried changing the formatting around ther SQL, but can't mess too much with the SQL code or it will change the results.

6
  • What is the error?
    – JNevill
    Commented Jul 9 at 17:33
  • 3
    It looks like your SQL string is multi-line. You'll need to change it to a single line or use one of the methods described in the answers to this question: stackoverflow.com/questions/16624550/…
    – JNevill
    Commented Jul 9 at 17:34
  • Should I break it up using line continuations? Commented Jul 9 at 17:51
  • You can if you would like. I was always partial to concatenating each line to its own variable like in that second answer as it made things cleaner looking, but either route would work just fine. You just can't have multi-line strings in VBA, which is a shame.
    – JNevill
    Commented Jul 9 at 18:00
  • 2
    Microsoft SQL Server (the sql-server tag) is not the same thing as Oracle. Your connection string uses an Oracle driver, your query uses Oracle's TO_DATE() function. Please tag your question correctly so that it's seen by the right people. Commented Jul 9 at 22:07

0

Browse other questions tagged or ask your own question.