Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/02/2009, 11:43
Avatar de [NiRVaNa]
[NiRVaNa]
 
Fecha de Ingreso: abril-2004
Ubicación: Someplace In The Middle Of Nowhere!
Mensajes: 325
Antigüedad: 20 años
Puntos: 6
Uso correcto de Case dentro de una Función

Buenas gente del foro...

Estoy armando una funcion del tipo trigger en la cual necesito hacer uso de alguna funcion condicional al mejor estilo "select case o switch"... Sé que existe una funcion llamada case dentro de postgres, pero nunca la habia usado en una funcion, solo en consultas...

Tengo armado esto hasta ahora, pero me marca error ni bien entra a ciclo del case:
Código:
CREATE OR REPLACE FUNCTION cargarOtros()
  RETURNS "trigger" AS
$BODY$
BEGIN
IF ((TG_OP = 'INSERT') OR (TG_OP = 'UPDATE')) THEN
	
	CASE
	-- Nivel de Estudios 
		WHEN new.otrainstisec is not null THEN
			IF ((select count(*) from institucion where nombre = new.otrainstisec) = 0) THEN
				insert into institucion (nombre, tipoinst) values (new.otrainstisec, 'SEC');
			END IF;
			
		WHEN new.otroinstiterc is not null THEN
			IF ((select count(*) from institucion where nombre = new.otroinstiterc) = 0) THEN
				insert into institucion (nombre, tipoinst) values (new.otroinstiterc, 'UNI');
			END IF;
			
		WHEN new.otroinstimae is not null THEN
			IF ((select count(*) from institucion where nombre = new.otroinstimae) = 0) THEN
				insert into institucion (nombre, tipoinst) values (new.otroinstimae, 'ESP');
			END IF;
			
		WHEN new.otrainstidoc is not null THEN
			IF ((select count(*) from institucion where nombre = new.otrainstidoc) = 0) THEN
				insert into institucion (nombre, tipoinst) values (new.otrainstidoc, 'ESP');
			END IF;
END;
	
END IF;
RETURN NEW;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;
El error que me marca es:
Cita:
ERROR: syntax error at or near "CASE" at character 1
QUERY: CASE WHEN $1 is not null THEN IF ((select count(*) from institucion where nombre = $2 ) = 0) THEN insert into institucion (nombre, tipoinst) values ( $3 , 'SEC')
El case lo uso a modo de validación y para no hacer uso excesivo de IF...

Es posible hacer loque estoy intentando hacer? O me tendre que conformar con el IF...

Salu2...
__________________
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

Última edición por [NiRVaNa]; 24/02/2009 a las 11:45 Razón: Se me chispoteo el titulo...