convert a process stored in sqlServer to Postgresql

1

Good evening I have here processes stored in sqlserver and I would like to implement them in postgres , I just need you to please explain what is the output element in postgres and the varable as there are many and it would not be correct that you put them all here

GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[spanular_ingreso]
@idingreso int
as
update ingreso set estado='ANULADO'
where idingreso=@idingreso


GO
CREATE proc [dbo].[spbuscar_ingreso_fecha]
@textobuscar varchar(50),
@textobuscar2 varchar(50)
as
SELECT ingreso.idingreso,
(trabajador.apellidos +' '+ trabajador.nombre) as Trabajador, 
proveedor.razon_social  as proveedor,
ingreso.fecha, ingreso.tipo_comprobante, 
ingreso.serie, ingreso.correlativo,
ingreso.estado, sum(detalle_ingreso.precio_compra*
detalle_ingreso.stock_inicial) as Total,ingreso.igv as Impuesto
FROM detalle_ingreso INNER JOIN ingreso 
ON detalle_ingreso.idingreso = ingreso.idingreso 
INNER JOIN proveedor 
ON ingreso.idproveedor = proveedor.idproveedor 
INNER JOIN dbo.trabajador 
ON ingreso.idtrabajador = trabajador.idtrabajador
group by
ingreso.idingreso,
trabajador.apellidos +' '+ trabajador.nombre, 
proveedor.razon_social,
ingreso.fecha, ingreso.tipo_comprobante, 
ingreso.serie, ingreso.correlativo,
ingreso.estado,ingreso.igv
having ingreso.fecha>=@textobuscar and ingreso.fecha<=@textobuscar2

GO

one simple and one complex so that I stay pretty clear, thank you very much

    
asked by Gilberto Asuaje 11.12.2016 в 03:13
source

1 answer

4

In PostgreSQL, there is no distinction between a stored procedure and a function . Both are defined as functions.

1. Simple Stored Procedure

In this case, since it is only an SQL statement, but without the need to return any value, you can define it as a function that returns void and you can use the language SQL :

create function spanular_ingreso(paramIdIngreso int)
returns void as $$
  update ingreso set estado='ANULADO'
  where idingreso=paramIdIngreso;
$$ language sql;

Although it does not seem intuitive, to execute this function, you can do it with a SELECT :

select spanular_ingreso(123);

Simplified demo .

If you are annoyed at using SELECT to execute a function that returns nothing, you can run it using the keyword PERFORM inside an anonymous block:

do $$
  begin
    perform spanular_ingreso(123);
  end
$$;

Simplified demo .

2. Complex Stored Procedure

Although you say that this stored procedure is more complex, in reality it is only a simple SQL query, so, again, you can define a function with the SQL language. The difference this time is that the clause RETURNS does not say void , but TABLE(...) with the names of the columns and their types that the function will return. I do not know the types of your tables, so I put something for the example, but you would have to adjust it to the correct types. ( Note: I find it odd that you are using parameters of varchar to compare them with a field that is called fecha .) It also makes no sense for the comparison to be made in HAVING instead of WHERE But I will ignore those details and others in this example ):

create function spbuscar_ingreso_fecha(textobuscar varchar(50), textobuscar2 varchar(50))
returns table(
          idingreso int,
          Trabajador varchar(100),
          proveedor varchar(100),
          fecha varchar(50),
          tipo_comprobante varchar(50),
          serie int,
          correlativo int,
          estado varchar(50),
          Total numeric(10,2),
          Impuesto int)
as $$
  SELECT ingreso.idingreso,
  (trabajador.apellidos || ' ' || trabajador.nombre) as Trabajador, 
  proveedor.razon_social  as proveedor,
  ingreso.fecha, ingreso.tipo_comprobante, 
  ingreso.serie, ingreso.correlativo,
  ingreso.estado, sum(detalle_ingreso.precio_compra*
  detalle_ingreso.stock_inicial) as Total,ingreso.igv as Impuesto
  FROM detalle_ingreso INNER JOIN ingreso 
  ON detalle_ingreso.idingreso = ingreso.idingreso 
  INNER JOIN proveedor 
  ON ingreso.idproveedor = proveedor.idproveedor 
  INNER JOIN dbo.trabajador 
  ON ingreso.idtrabajador = trabajador.idtrabajador
  group by
  ingreso.idingreso,
  trabajador.apellidos || ' ' || trabajador.nombre, 
  proveedor.razon_social,
  ingreso.fecha, ingreso.tipo_comprobante, 
  ingreso.serie, ingreso.correlativo,
  ingreso.estado,ingreso.igv
  having ingreso.fecha>=textobuscar and ingreso.fecha<=textobuscar2;
$$ language sql;

And to execute this function, you would do it this way:

select * from spbuscar_ingreso_fecha('2010-01-01', '2012-01-01')

Simplified demo .

    
answered by 11.12.2016 / 06:27
source