Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » PostgreSQL »

insertando en campos tipo array

Estas en el tema de insertando en campos tipo array en el foro de PostgreSQL en Foros del Web. Hola a todos tengo un problema al insertar en una tabla que tiene un campo array de un tipo definido por el usuario, expongo mi ...
  #1 (permalink)  
Antiguo 10/03/2010, 10:03
 
Fecha de Ingreso: septiembre-2008
Mensajes: 75
Antigüedad: 15 años, 7 meses
Puntos: 1
insertando en campos tipo array

Hola a todos tengo un problema al insertar en una tabla que tiene un campo array de un tipo definido por el usuario, expongo mi script

--Creo mi tipo de dato
create type td_detalle_prestamo as
(
cuenta character varying(15),
importe real,
otracuenta character varying(15),
aplicacion character(1),
cuentaaplica character varying(15)
);

--Creo mi tabla
CREATE TABLE prestamocab
(
codempresa character(3) NOT NULL,
tipo character(1) not null,
codigoreg character varying(50) not null,
codanexo character varying(6) NOT NULL,
fecha date not null,
moneda character(2) DEFAULT '1'::bpchar,
tcambio real not null,
total real not null,
porcinicial real not null,
cuotainicial real not null,
afectaigv character(1) not null,
nrocuotas int not null,
usuario character varying(20) NOT NULL,
fechareg timestamp without time zone DEFAULT now(),
estado character(1) not null,
detalle td_detalle_prestamo[],
CONSTRAINT "PK_prestamocab" PRIMARY KEY (codigoreg),
CONSTRAINT "CH_prestamocab_tipo" CHECK (tipo = 'L'::bpchar OR tipo = 'P'::bpchar OR tipo = 'O'::bpchar)
)
WITH (OIDS=FALSE);

--Insertar registro
insert into prestamocab(codempresa,tipo,codigoreg,codanexo,fec ha,moneda,tcambio,total,porcinicial,cuotainicial,a fectaigv,nrocuotas,usuario,estado,detalle)
values('001', 'P', '04714','000001', '2010-03-01', '1', 1, 50000, 10, 5000, 'S', 6,
'ADMIN', 'A',ARRAY[ROW('10',100,'10','S','10'),ROW('12',200,'12','S', '12')]);

--Error
ERROR: column "detalle" is of type td_detalle_prestamo[] but expression is of type record[]
LINEA 4: 'ADMIN', 'A',ARRAY[ROW('10',0,'10','S','10')]);
^
HINT: You will need to rewrite or cast the expression.

gracias
  #2 (permalink)  
Antiguo 10/03/2010, 12:21
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: insertando en campos tipo array

En la creación de la tabla, aca se genera un error
detalle td_detalle_prestamo[],
td_detalle_prestamo es un tipo de dato declarado como registro y no entiendo por que lo declaras como corchete.

Realmente como lo tienes declarado?

saludos ediwsp
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #3 (permalink)  
Antiguo 10/03/2010, 14:18
 
Fecha de Ingreso: septiembre-2008
Mensajes: 75
Antigüedad: 15 años, 7 meses
Puntos: 1
Respuesta: insertando en campos tipo array

pues estoy tratando de declararlo como un array del tipo de dato que yo he creado, me sorprende porque yo si he podido crear la tabla sin ningun tipo de error

es mas antes hice esto:

CREATE TABLE prestamocab
(
codempresa character(3) NOT NULL,
tipo character(1) not null,
codigoreg character varying(50) not null,
codanexo character varying(6) NOT NULL,
fecha date not null,
moneda character(2) DEFAULT '1'::bpchar,
tcambio real not null,
total real not null,
porcinicial real not null,
cuotainicial real not null,
afectaigv character(1) not null,
nrocuotas int not null,
usuario character varying(20) NOT NULL,
fechareg timestamp without time zone DEFAULT now(),
estado character(1) not null,
detalle td_detalle_prestamo,
CONSTRAINT "PK_prestamocab" PRIMARY KEY (codigoreg),
CONSTRAINT "CH_prestamocab_tipo" CHECK (tipo = 'L'::bpchar OR tipo = 'P'::bpchar OR tipo = 'O'::bpchar)
)
WITH (OIDS=FALSE);

pero en este caso solo me aceptaria un solo detalle y mi idea es tener un array de detalle en esa tabla

gracias por la atencion
  #4 (permalink)  
Antiguo 10/03/2010, 16:09
Avatar de huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 15 años, 2 meses
Puntos: 360
Respuesta: insertando en campos tipo array

Tenes razón.... en la versión 8.2 me genera un error en la creación de la tabla, pero en la 8.4 me la acepta.

Debes castear el array al tipo de dato con el que fue generado el campo.

Prueba así

Código SQL:
Ver original
  1. INSERT INTO prestamocab
  2. (codempresa,tipo,codigoreg,codanexo,fecha,moneda,
  3. tcambio,total,porcinicial,cuotainicial,afectaigv,nrocuotas,
  4. usuario,estado,detalle)
  5. VALUES('001', 'P', '04714','000001', '2010-03-01', '1', 1,
  6. 50000, 10, 5000, 'S', 6,'ADMIN', 'A',
  7. ARRAY[ROW('10',100,'10','S','10'),ROW('12',200,'12','S', '12')]::td_detalle_prestamo[]);

Lo que se hace es castear un tipo record[] a td_detalle_prestamo[] previamente declarado.

saludos
__________________
Without data, You are another person with an opinion.
W. Edwads Deming
  #5 (permalink)  
Antiguo 11/03/2010, 07:21
 
Fecha de Ingreso: septiembre-2008
Mensajes: 75
Antigüedad: 15 años, 7 meses
Puntos: 1
Respuesta: insertando en campos tipo array

Excelente huesos intente castearlo pero me falta el bendito []

gracias, muchas

Etiquetas: campos, tipo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:35.