Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a table variable in a procedure that inherits a type that has already been created previously on postgresql?

I am doing a migration exercise from SQL Server to Postgres and I found this type of variable in a stored procedure:

CREATE     PROCEDURE [dbo].[CambiodedepositanteTIDIS_CERTS]
@Client(50),
@Email varchar(50),
@Planilla1 [dbo].[TipoPlanilla1]    Readonly 

In the variable planila1 , the type TipoPanilla1 is being inherited, if I understood correctly, this is the type:

CREATE TYPE [dbo].[TipoPlanilla1] AS TABLE(
    [CuentaDeposito] [bigint] NULL,
    [CodigoOyd] [decimal](18, 0) NULL,
    [Especie] [varchar](150) NULL,
    [Isin] [varchar](50) NULL,
    [Emision] [decimal](18, 0) NULL,
    [ValorTrasladar] [decimal](18, 0) NULL
)

Is there a way to do something similar in postgress?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Postgres has no "table variables", but you could pass an array of a type.

CREATE TYPE tipoplanilla1 AS 
(
  cuentadeposito bigint,
  codigooyd decimal(18, 0),
  especie varchar(150),
  isin varchar(50),
  emision decimal(18, 0),
  valortrasladar decimal(18, 0)
);

Then declare a parameter as an array:

create procedure cambiodedepositantetidis_certs(
    client text, email text, planilla1 tipoplanilla1[])
as
$$
begin
....
end
$$
language plpgsql;

Note that for every table that is created, Postgres automatically creates a type with the same name. So if you want to pass "rows" of a table, there is no need to create an extra type. You can use the table’s name as the parameter type.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading