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.
sql-server
tag) is not the same thing as Oracle. Your connection string uses an Oracle driver, your query uses Oracle'sTO_DATE()
function. Please tag your question correctly so that it's seen by the right people.