迁移到pgsql,在select into的函数中出错

时间:2017-12-13 21:38:49

标签: postgresql plpgsql database-migration

我从sql-server迁移到pgsql。 我使用在java中创建的脚本来做到这一点,但是当我尝试将此过程(sql-server)转换为函数并在bd(pgsql)中进行测试时,控制台在部件#paso1中显示错误。

这是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

这是为pgsql生成的代码(错误在变量#paso1中,但我不知道在该行之后是否存在更多错误。):

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;

控制台显示:

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

但我认为这并不重要,因为我知道这不是做那种功能的方法......我不知道怎么做corectly

1 个答案:

答案 0 :(得分:0)

该功能有多个错误:

  1. String concatenation在SQL中使用||完成。因此substring(b.codigocuenta, 1, 1) + '000000'应为substring(b.codigocuenta, 1, 1) || '000000'
  2. Identifiers不允许以#开头。所以#paso1是非法变量名称
  3. 您没有declare变量#paso1
  4. 您可以在单个变量中not store多行,因此您希望在过程中返回查询结果。
  5. return a result,您需要将该功能声明为returns table而不是returns void
  6. 要从PL / pgSQL函数返回结果集,您需要使用RETURN QUERY。你不能只写一个select语句,也不能对结果做任何事情。
  7. 但是你不需要PL / pgSQL来返回查询结果。一个SQL函数就足够了。

    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;
    

    要使用该功能,您可以使用:

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