Consulta SQL retornando erro!

fviriato

17-09-2016 13:58:12

Galera, estou ha uns 3 dias tentando solucionar o seguinte erro no VBA Excel:

Erro em tempo de execução '-2147467259 (80004005)':
ORA-00907: parêntese direito não encontrado


Segue a string abaixo:

Sub mConsulta()

Dim sql As String
Dim linha As Integer

Conectar_db

sql = "SELECT PLC_NOCONTAB AS CONTA,SUBSTR(MONTAMASCARA(PLC_NOCONTAB, '9.9.99.99.999'),1,30) AS PLC_NOCONTABMSK," _
& "PLC_DSCONTAB AS DESCRICAO," _
& "PLC_GRAU, PLC_SA, PLC_CONTRESUL, PLC_NATCONT, PLC_NOREDUZ AS CODREDUZ," _
& "SUM(DEBITO) AS DEBITO," _
& "SUM(CREDITO) AS CREDITO," _
& "SUM(SALDO) As Saldo FROM PLANOCTA_PLC," _
& "(SELECT CMES_NOCONTAB, PLC_CODPLANO AS PLANO," _
& "NVL(SUM(DEBITO),0) AS DEBITO, NVL(SUM(CREDITO),0) AS CREDITO, NVL(SUM(DEBITO - CREDITO), 0) As SALDO FROM PLANOCTA_PLC A, CUSTOMES_MEN B" _
& "WHERE PLC_CODPLANO ='15' AND PLC_NOCONTAB >='5' AND PLC_NOCONTAB <='7' AND B.CMES_CDEMPRESA ='01' AND B.ANOMES ='201605' AND B.CMES_NOCONTAB = A.PLC_NOCONTAB AND B.CMES_PLCONTAB = A.PLC_CODPLANO AND A.PLC_SA = 'A' GROUP BY CMES_NOCONTAB, PLC_CODPLANO)" _
& "WHERE PLC_CODPLANO ='15' AND PLC_CODPLANO = PLANO AND SUBSTR(CMES_NOCONTAB,1,LENGTH(PLC_NOCONTAB))=PLC_NOCONTAB AND PLC_GRAU <= '3' AND ((CREDITO <> 0) OR (DEBITO <> 0)) AND PLC_NOCONTAB >= '5' AND PLC_NOCONTAB <= '7'" _
& "GROUP BY PLC_NOCONTAB, PLC_DSCONTAB, PLC_GRAU, PLC_SA, PLC_CONTRESUL, PLC_NATCONT, PLC_NOREDUZ ORDER BY PLC_NOCONTAB;"

Set Consulta = Conexao.Execute(sql)

linha = 1

If Not Consulta.EOF Then
Do While Not Consulta.EOF
Plan3.Range("A" & linha) = Consulta!PLC_NOCONTAB
Plan3.Range("B" & linha) = Consulta!DESCRICAO
Plan3.Range("C" & linha) = Consulta!DEBITO
Plan3.Range("D" & linha) = Consulta!CREDITO
Plan3.Range("E" & linha) = Consulta!SALDO
linha = linha + 1
Consulta.MoveNext
Loop
End If

End Sub

Reinaldo

17-09-2016 16:45:25

Muito dificil somente com a string sql localizar algum problema.
A unica "coisa" que transparece e um erro no trecho abaixo, onde apos customes_men B, deveria haver um espaço, pois assim "B" esta colando no Where dalinha de baixo -->BWHERE
"& "NVL(SUM(DEBITO),0) AS DEBITO, NVL(SUM(CREDITO),0) AS CREDITO, NVL(SUM(DEBITO - CREDITO), 0) As SALDO FROM PLANOCTA_PLC A, CUSTOMES_MEN B" _
& "WHERE...."
Experimente assim:
"& "NVL(SUM(DEBITO),0) AS DEBITO, NVL(SUM(CREDITO),0) AS CREDITO, NVL(SUM(DEBITO - CREDITO), 0) As SALDO FROM PLANOCTA_PLC A, CUSTOMES_MEN B " _
& "WHERE...."

fviriato

18-09-2016 20:28:12

Reinaldo, sensacional!!!!!!

Era isso mesmo, acertou na mosca..................Muito obrigado pela ajuda!!!!!

Abraços