2017-12-13 6 views
-1

ich bin migrieren von sql-server zu pgsql. Ich benutze ein Skript in Java für das, aber wenn ich versuche, diese Prozedur (sql-Server) in eine Funktion zu konvertieren und testen in der bd (pgsql) die Konsole zeigen Fehler im Teil # Paso1.migration nach pgsql, fehler in einer funktion mit in

Dies ist der Code in SQL-Server:

/****** Object: StoredProcedure [dbo].[paBalanceClasificado] Script Date: 30/11/2017 16:38:42 ******/ 
SET ANSI_NULLS OFF 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 
CREATE PROCEDURE [dbo].[paBalanceClasificado] 
@empresa int, 
@fecha1 smalldatetime, 
@fecha2 smalldatetime 

AS 

SELECT  SUBSTRING(b.codigoCuenta, 1, 1) + '000000' AS Codigo, SUM(a.montoDebe) AS Debe, SUM(a.montoHaber) AS Haber 
INTO #PASO1 
FROM   movimientosContables a INNER JOIN 
         CuentasContables b ON a.codigoCorto = b.codigoCorto AND b.codigoEmpresa = @empresa 
WHERE  (a.codigoEmpresa = @empresa and fechaProceso between @fecha1 and @fecha2) 
GROUP BY SUBSTRING(b.codigoCuenta, 1, 1) + '000000' 
UNION ALL 
SELECT  SUBSTRING(b.codigoCuenta, 1, 2) + '00000' AS Codigo, SUM(a.montoDebe) AS Debe, SUM(a.montoHaber) AS Haber 
FROM   movimientosContables a INNER JOIN 
         CuentasContables b ON a.codigoCorto = b.codigoCorto AND b.codigoEmpresa = @empresa 
WHERE  (a.codigoEmpresa = @empresa and fechaProceso between @fecha1 and @fecha2) 
GROUP BY SUBSTRING(b.codigoCuenta, 1, 2) + '00000' 
UNION ALL 
SELECT  SUBSTRING(b.codigoCuenta, 1, 4) + '000' AS Codigo, SUM(a.montoDebe) AS Debe, SUM(a.montoHaber) AS Haber 
FROM   movimientosContables a INNER JOIN 
         CuentasContables b ON a.codigoCorto = b.codigoCorto AND b.codigoEmpresa = @empresa 
WHERE  (a.codigoEmpresa = @empresa and fechaProceso between @fecha1 and @fecha2) 
GROUP BY SUBSTRING(b.codigoCuenta, 1, 4) + '000' 
UNION ALL 
SELECT  SUBSTRING(b.codigoCuenta, 1, 7) AS Codigo, SUM(a.montoDebe) AS Debe, SUM(a.montoHaber) AS Haber 
FROM   movimientosContables a INNER JOIN 
         CuentasContables b ON a.codigoCorto = b.codigoCorto AND b.codigoEmpresa = @empresa 
WHERE  (a.codigoEmpresa = @empresa and fechaProceso between @fecha1 and @fecha2) 
GROUP BY SUBSTRING(b.codigoCuenta, 1,7) 
ORDER BY codigo 

SELECT  codigo, nombreCuenta, debe, haber,codigocorto 
FROM   #Paso1, cuentascontables 
WHERE  codigocuenta = codigo AND codigoempresa = @empresa --and 
SUBSTRING(codigoCuenta, 5,3)='000' 
order by codigo 
GO 

und dies ist der für pgsql generierten Code (der Fehler in der Variable # paso1 ist, aber ich weiß nicht, ob nach dieser Zeile mehr Fehler vorhanden ist.) :

CREATE OR REPLACE FUNCTION pabalanceclasificado(
p_empresa int , 
p_fecha1 timestamp(0) , 
p_fecha2 timestamp(0) 
) 
    RETURNS VOID 
AS $$ 
    DECLARE 
    BEGIN 
      select  substring(b.codigocuenta, 1, 1) + '000000' as codigo, sum(a.montodebe) as debe, sum(a.montohaber) as haber 
     into #paso1 
     from   movimientoscontables a inner join 
           cuentascontables b on a.codigocorto = b.codigocorto and b.codigoempresa = p_empresa 
     where  (a.codigoempresa = p_empresa and fechaproceso between p_fecha1 and p_fecha2) 
     group by substring(b.codigocuenta, 1, 1) + '000000' 
     union all 
     select  substring(b.codigocuenta, 1, 2) + '00000' as codigo, sum(a.montodebe) as debe, sum(a.montohaber) as haber 
     from   movimientoscontables a inner join 
           cuentascontables b on a.codigocorto = b.codigocorto and b.codigoempresa = p_empresa 
     where  (a.codigoempresa = p_empresa and fechaproceso between p_fecha1 and p_fecha2) 
     group by substring(b.codigocuenta, 1, 2) + '00000' 
     union all 
     select  substring(b.codigocuenta, 1, 4) + '000' as codigo, sum(a.montodebe) as debe, sum(a.montohaber) as haber 
     from   movimientoscontables a inner join 
           cuentascontables b on a.codigocorto = b.codigocorto and b.codigoempresa = p_empresa 
     where  (a.codigoempresa = p_empresa and fechaproceso between p_fecha1 and p_fecha2) 
     group by substring(b.codigocuenta, 1, 4) + '000' 
     union all 
     select  substring(b.codigocuenta, 1, 7) as codigo, sum(a.montodebe) as debe, sum(a.montohaber) as haber 
     from   movimientoscontables a inner join 
           cuentascontables b on a.codigocorto = b.codigocorto and b.codigoempresa = p_empresa 
     where  (a.codigoempresa = p_empresa and fechaproceso between p_fecha1 and p_fecha2) 
     group by substring(b.codigocuenta, 1,7) 
     order by codigo 
     select  codigo, nombrecuenta, debe, haber,codigocorto 
     from   #paso1, cuentascontables 
     where  codigocuenta = codigo and codigoempresa = p_empresa --and substring(codigocuenta, 5,3)='000' 
     order by codigo 
END; 
$$ LANGUAGE plpgsql; 

die Konsole zeigt:

ERROR: error de sintaxis en o cerca de «#» 
LÍNEA 11: into #paso1 
> Terminated with exit code 0. 

Aber ich denke, das ist nicht wichtig, weil ich weiß th auf dies ist nicht die Art und Weise, dass funtion zu tun ... und ich weiß nicht, wie zu tun corectly

+0

Was ist die Fehlermeldung? Ich kann es in Ihrer Frage nicht finden. – DDeMartini

+0

die Konsole zeigt: FEHLER: Fehler de sintaxis en o cerca de # LINEA 11: in # paso1 > Mit Exit-Code 0 beendet. ABER ich denke, das ist nicht wichtig, weil ich weiß, dass dies nicht ist Der Weg, um diese Funktion zu tun ... und ich weiß nicht, wie doe corectly –

+0

Ich denke, das erste Problem mit 'in # paso1' ist, dass pgsql nicht das '#' Trennzeichen für eine Variable verwendet. Versuchen Sie, das an allen Orten nur in "Paso1" zu ändern. Wenn ich es überblicke, sieht es immer noch MS SQL-zentrisch aus ... könnte alle möglichen Probleme enthalten, aber ich würde damit anfangen, diesen Variablennamen zu ändern. – DDeMartini

Antwort

0

Es gibt mehrere Fehler in dieser Funktion:

  1. String concatenation wird mit || in SQL getan. Also substring(b.codigocuenta, 1, 1) + '000000' sollte substring(b.codigocuenta, 1, 1) || '000000' sein
  2. Identifiers dürfen nicht mit # starten. Also #paso1 ist eine ungültige Variable Name
  3. Sie nicht declare die Variable #paso1.
  4. Sie können not store mehrere Zeilen in einer einzigen Variablen, so dass es so aussieht, als ob Sie das Ergebnis der Abfrage in Ihrer Prozedur zurückgeben möchten.
  5. Um return a result Sie ein Ergebnis aus einer PL/pgSQL-Funktion eingestellt Rückkehr zu erklären, müssen die Funktion als returns table nicht returns void
  6. Sie benötigen würde RETURN QUERY zu verwenden. Sie können nicht einfach eine Select-Anweisung schreiben und nichts mit dem Ergebnis tun.

Sie benötigen jedoch PL/pgSQL nicht, um das Ergebnis einer Abfrage zurückzugeben. Eine SQL-Funktion ist genug.

CREATE OR REPLACE FUNCTION pabalanceclasificado(p_empresa int, p_fecha1 timestamp(0), p_fecha2 timestamp(0)) 
    RETURNS table (codigo varchar, debe bigint, bigint haber) 
AS $$ 
    select codigo, nombrecuenta, debe, haber,codigocorto 
    from (
     select  substring(b.codigocuenta, 1, 1) || '000000' as codigo, sum(a.montodebe) as debe, sum(a.montohaber) as haber 
     from movimientoscontables a 
     inner join cuentascontables b on a.codigocorto = b.codigocorto and b.codigoempresa = p_empresa 
     where  (a.codigoempresa = p_empresa and fechaproceso between p_fecha1 and p_fecha2) 
     group by substring(b.codigocuenta, 1, 1) || '000000' 

     union all 

     select  substring(b.codigocuenta, 1, 2) || '00000' as codigo, sum(a.montodebe) as debe, sum(a.montohaber) as haber 
     from   movimientoscontables a inner join 
          cuentascontables b on a.codigocorto = b.codigocorto and b.codigoempresa = p_empresa 
     where  (a.codigoempresa = p_empresa and fechaproceso between p_fecha1 and p_fecha2) 
     group by substring(b.codigocuenta, 1, 2) || '00000' 

     union all 

     select  substring(b.codigocuenta, 1, 4) || '000' as codigo, sum(a.montodebe) as debe, sum(a.montohaber) as haber 
     from movimientoscontables a 
     inner join cuentascontables b on a.codigocorto = b.codigocorto and b.codigoempresa = p_empresa 
     where  (a.codigoempresa = p_empresa and fechaproceso between p_fecha1 and p_fecha2) 
     group by substring(b.codigocuenta, 1, 4) || '000' 

     union all 
     select  substring(b.codigocuenta, 1, 7) as codigo, sum(a.montodebe) as debe, sum(a.montohaber) as haber 
     from   movimientoscontables a 
     inner join cuentascontables b on a.codigocorto = b.codigocorto and b.codigoempresa = p_empresa 
     where  (a.codigoempresa = p_empresa and fechaproceso between p_fecha1 and p_fecha2) 
     group by substring(b.codigocuenta, 1,7) 
    ) t 
    join cuentascontables 
     on codigocuenta = codigo 
     and codigoempresa = p_empresa --and substring(codigocuenta, 5,3)='000' 
    order by codigo; 
$$ 
LANGUAGE sql; 

Um diese Funktion nutzen Sie verwenden würden:

select * 
from pabalanceclasificado(42, current_timestamp, current_timestamp); 
+0

Ich führe deinen Code in Pgsql 9.6 und die Konsole zeigen: test.sql: 41: Fehler: Fehler de sintaxis en o cerca de  »select« LÍNEA 35: wählen codigo, nombrecuenta, debe, haber, codigocorto; ^ beendet mit Exit-Code 0. –

+0

@ JoséZuñiga: Entschuldigung, zu viel kopieren und einfügen von Ihrem Code. Siehe meine Bearbeitung –

Verwandte Themen