FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Problema con SQL Server - RESUELTO
Posts: 1279
Joined: Mon Feb 06, 2006 04:28 PM
Problema con SQL Server - RESUELTO
Posted: Thu May 29, 2025 12:45 PM
Me ha ocurrido en otras oportunidades y modificando el script ha funcionado, pero en este caso no lo he podido solucionar.......
Este es el Script en MS SQL SERVER
     SELECT a.fecha, a.tropaano, a.tropanumero, a.correlativo, ta.descripcion,
          ROUND(NULLIF(t.kilosenpie, 0) / NULLIF(t.cabezas, 0), 2) AS kilosenpie,
          (a.kilos + b.kilos) AS KgsEn2da,
          ROUND( ((a.kilos + b.kilos) / NULLIF(t.kilosenpie / NULLIF(t.cabezas, 0), 0)) * 100, 2 ) AS 'Rend.', 
          b.kosher AS [Ritual], IIF(a.hilton = 0x54, 'SI', 'NO') AS hilton,
          IIF(a.tipodehacienda = 0, 'NO', 'SI') AS organico, t.productor AS 'No.Prod',
          prd.nombre AS 'Productor', t.consignatario AS 'No.Con.', con.nombre AS 'Consignatario',
          t.dicoseorigen, dpt.nombre AS 'Departamento',
          IIF(t.moneda = 22, 'Dólar', 'Peso U.') AS Moneda,
          IIF(t.moneda = 22, ROUND(NULLIF(fh.subtotal, 0) / NULLIF(td.kilosen2, 0), 2),
               ROUND(NULLIF(fh.subtotal, 0) / NULLIF(td.kilosen2, 0) / NULLIF(fh.cotizaciondolar, 0), 2)
          ) AS 'Precio U$S'
     FROM C10304 a WITH (NOLOCK)
     INNER JOIN C10304 b WITH (NOLOCK)
          ON a.establecimiento = b.establecimiento AND a.fecha = b.fecha AND a.correlativo = b.correlativo
          AND b.estado < 5 AND b.tipoderegistro = 4 AND b.mediares = 2
     INNER JOIN C10203 t WITH (NOLOCK) ON t.empresa = a.empresa AND t.especie = a.tropaespecie
          AND t.ano = a.tropaano AND t.numero = a.tropanumero
     INNER JOIN C10204 td WITH (NOLOCK) ON td.empresa = a.empresa AND td.especie = a.tropaespecie
          AND td.ano = a.tropaano AND td.numero = a.tropanumero AND td.ordendepesada = a.tropaorden
     LEFT JOIN P0025 prd WITH (NOLOCK) ON prd.establecimiento = a.establecimiento 
          AND prd.codigo = t.productor
     LEFT JOIN P0025 con WITH (NOLOCK) ON con.establecimiento = a.establecimiento 
          AND con.codigo = t.consignatario
     LEFT JOIN P0015 dpt WITH (NOLOCK)
          ON dpt.codigo = prd.departamento
     LEFT JOIN P0021 ta WITH (NOLOCK)
          ON ta.codigo = a.tipodeanimal
     OUTER APPLY ( 
          SELECT MIN(fc.cotizaciondolar) AS cotizaciondolar, SUM(fd.subtotal) AS subtotal
               FROM C10210 fc WITH (NOLOCK)
          INNER JOIN C10211 fd WITH (NOLOCK) ON fd.empresa = fc.empresa AND fd.pof = fc.pof
               AND fd.numero = fc.numero AND fd.tropalinea = td.ordendepesada
          WHERE fc.empresa = a.empresa AND fc.estado < 9 AND fc.pof = 'P' AND fc.tropaespecie = a.tropaespecie
               AND fc.tropaano = a.tropaano AND fc.tropanumero = a.tropanumero
          ) fh
     LEFT JOIN P0025 dio WITH (NOLOCK)
          ON dio.establecimiento = a.establecimiento 
          AND dio.dicose = t.dicoseorigen
     WHERE a.empresa = 1 AND a.estado < 5 AND a.tipoderegistro = 4 AND a.mediares = 1 
          AND a.fecha BETWEEN '20250301' AND '20250522'
     ORDER BY a.fecha, a.tropaano, a.tropanumero, a.correlativo;
Lo cargo en HeidiSQL y funciona perfectamente.



Pero con este codigo, no funciona:
text into cSql
     
     SELECT a.fecha, a.tropaano, a.tropanumero, a.correlativo, ta.descripcion,
          ROUND(NULLIF(t.kilosenpie, 0) / NULLIF(t.cabezas, 0), 2) AS kilosenpie,
          (a.kilos + b.kilos) AS KgsEn2da,
          ROUND( ((a.kilos + b.kilos) / NULLIF(t.kilosenpie / NULLIF(t.cabezas, 0), 0)) * 100, 2 ) AS 'Rend.', 
          b.kosher AS [Ritual], IIF(a.hilton = 0x54, 'SI', 'NO') AS hilton,
          IIF(a.tipodehacienda = 0, 'NO', 'SI') AS organico, t.productor AS 'No.Prod',
          prd.nombre AS 'Productor', t.consignatario AS 'No.Con.', con.nombre AS 'Consignatario',
          t.dicoseorigen, dpt.nombre AS 'Departamento',
          IIF(t.moneda = 22, 'Dólar', 'Peso U.') AS Moneda,
          IIF(t.moneda = 22, ROUND(NULLIF(fh.subtotal, 0) / NULLIF(td.kilosen2, 0), 2),
               ROUND(NULLIF(fh.subtotal, 0) / NULLIF(td.kilosen2, 0) / NULLIF(fh.cotizaciondolar, 0), 2)
          ) AS 'Precio U$S'
     FROM C10304 a WITH (NOLOCK)
     INNER JOIN C10304 b WITH (NOLOCK)
          ON a.establecimiento = b.establecimiento AND a.fecha = b.fecha AND a.correlativo = b.correlativo
          AND b.estado < 5 AND b.tipoderegistro = 4 AND b.mediares = 2
     INNER JOIN C10203 t WITH (NOLOCK) ON t.empresa = a.empresa AND t.especie = a.tropaespecie
          AND t.ano = a.tropaano AND t.numero = a.tropanumero
     INNER JOIN C10204 td WITH (NOLOCK) ON td.empresa = a.empresa AND td.especie = a.tropaespecie
          AND td.ano = a.tropaano AND td.numero = a.tropanumero AND td.ordendepesada = a.tropaorden
     LEFT JOIN P0025 prd WITH (NOLOCK) ON prd.establecimiento = a.establecimiento 
          AND prd.codigo = t.productor
     LEFT JOIN P0025 con WITH (NOLOCK) ON con.establecimiento = a.establecimiento 
          AND con.codigo = t.consignatario
     LEFT JOIN P0015 dpt WITH (NOLOCK)
          ON dpt.codigo = prd.departamento
     LEFT JOIN P0021 ta WITH (NOLOCK)
          ON ta.codigo = a.tipodeanimal
     OUTER APPLY ( 
          SELECT MIN(fc.cotizaciondolar) AS cotizaciondolar, SUM(fd.subtotal) AS subtotal
               FROM C10210 fc WITH (NOLOCK)
          INNER JOIN C10211 fd WITH (NOLOCK) ON fd.empresa = fc.empresa AND fd.pof = fc.pof
               AND fd.numero = fc.numero AND fd.tropalinea = td.ordendepesada
          WHERE fc.empresa = a.empresa AND fc.estado < 9 AND fc.pof = 'P' AND fc.tropaespecie = a.tropaespecie
               AND fc.tropaano = a.tropaano AND fc.tropanumero = a.tropanumero
          ) fh
     LEFT JOIN P0025 dio WITH (NOLOCK)
          ON dio.establecimiento = a.establecimiento 
          AND dio.dicose = t.dicoseorigen
     WHERE a.empresa = 1 AND a.estado < 5 AND a.tipoderegistro = 4 AND a.mediares = 1 
          AND a.fecha BETWEEN '20250301' AND '20250522'
     ORDER BY a.fecha, a.tropaano, a.tropanumero, a.correlativo;
        
     ENDTEXT 
            
     oRs := FW_OpenRecordSet( oCn2, cSql )

     IF !oRs:Eof()
          xBrowse( oRs )
          oRs:Close()
     ELSE
          MsgAlert( 'No se pudo conectar con la base de datos' ) 
     ENDIF

     ENDTEXT
siendo el script un copy paste del de heidisql resulta en este browse vacío:



Que puede causar este problema?
Saludos/Regards,

José Murugosa

"Los errores en programación, siempre están entre la silla, el teclado y la IA!!"
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Problema con SQL Server
Posted: Thu May 29, 2025 05:56 PM

Estimado José,

Has probado a quitar la línea en blanco que tienes justo despues de text into cSql ? Y también la del final antes de ENDTEXT ?

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1279
Joined: Mon Feb 06, 2006 04:28 PM
Re: Problema con SQL Server
Posted: Thu May 29, 2025 06:28 PM
Gracias por tu respuesta Antonio,
Probé de eliminar las líneas y hasta las tabulaciones y el mismo resultado :(
Saludos/Regards,

José Murugosa

"Los errores en programación, siempre están entre la silla, el teclado y la IA!!"
Posts: 61
Joined: Wed Jul 26, 2023 12:08 PM
Re: Problema con SQL Server
Posted: Thu May 29, 2025 07:18 PM
Dear Jose,

Please change your query method with this,

TEXT INTO cSQL
...
ENDTEXT

oRS := oCn2:Execute( cSQL )
xBrowse( oRS )

and please check this
xBrowse( oCn2:aSQL ) aSQL is last executed 10 sql queries with result in fivewin SQL parser

Best regards,
Ertan

ertan.ozturk@yahoo.com
Best regards,
Ertan,

ertan_ozturk@yahoo.com
Posts: 1279
Joined: Mon Feb 06, 2006 04:28 PM
Re: Problema con SQL Server
Posted: Fri May 30, 2025 11:26 AM

Dear Ertan,

You were write!!

I just change the line:

oRs := FW_OpenRecordSet( oCn2, cSql )

with:

oRs := oCn2:Execute( cSQL )

And I got the wright result.

Thankyou very much!!!

Saludos/Regards,

José Murugosa

"Los errores en programación, siempre están entre la silla, el teclado y la IA!!"
Posts: 61
Joined: Wed Jul 26, 2023 12:08 PM
Re: Problema con SQL Server - RESUELTO
Posted: Fri May 30, 2025 12:54 PM

Dear Jose,

aResult := oCn2:Execute( cSQL )

oRs := oCn2:RowSet( cSQL )

Likewise. Sincerely,

Ertan

Best regards,
Ertan,

ertan_ozturk@yahoo.com
Posts: 1279
Joined: Mon Feb 06, 2006 04:28 PM
Re: Problema con SQL Server - RESUELTO
Posted: Tue Jun 03, 2025 05:21 PM
ertan wrote: Dear Jose,

aResult := oCn2:Execute( cSQL )
oRs := oCn2:RowSet( cSQL )

Likewise. Sincerely,

Ertan
Thanks a lot
Saludos/Regards,

José Murugosa

"Los errores en programación, siempre están entre la silla, el teclado y la IA!!"
Posts: 1279
Joined: Mon Feb 06, 2006 04:28 PM
Re: Problema con SQL Server - RESUELTO
Posted: Wed Jun 04, 2025 04:02 PM
jose_murugosa wrote:
Dear Jose,

aResult := oCn2:Execute( cSQL )
oRs := oCn2:RowSet( cSQL )

Likewise. Sincerely,

Ertan
Thanks a lot
Dear Ertan

Remember we are working with sql server, so we works with ADO recordsets
The problem was that I was mixing commands from the class of mysql of fivewin with a recordset ADO.

The Fivewin Class for MySql has an execute method that returns an array but on ADO this method returns a recordset.
Thats the reason it works.

"The Execute method for commands that return results (Recordset):
This method is used to execute SQL queries that return data, such as SELECT.
The query results are stored in a new Recordset object.
If the query returns rows, the results are stored in the Recordset, allowing navigation and access to the data."

I must avoid to confuse one command with the other, It happens to me some times.

Thanks a lot for your great help
Saludos/Regards,

José Murugosa

"Los errores en programación, siempre están entre la silla, el teclado y la IA!!"
Posts: 61
Joined: Wed Jul 26, 2023 12:08 PM
Re: Problema con SQL Server - RESUELTO
Posted: Wed Jun 04, 2025 04:34 PM
Dear Jose,

Your welcome :)
Best regards,
Ertan,

ertan_ozturk@yahoo.com

Continue the discussion