ClubEnsayos.com - Ensayos de Calidad, Tareas y Monografias
Buscar

Sentencias ASGBD


Enviado por   •  16 de Febrero de 2019  •  Tareas  •  1.344 Palabras (6 Páginas)  •  61 Visitas

Página 1 de 6

19.2.1. CREATE PROCEDURE y CREATE FUNCTION

CREATE PROCEDURE sp_name ([parameter[,...]])

    [characteristic ...] routine_body

CREATE FUNCTION sp_name ([parameter[,...]])

    RETURNS type

    [characteristic ...] routine_body

parameter:

    [ IN | OUT | INOUT ] param_name type

type:

    Any valid MySQL data type

characteristic:

    LANGUAGE SQL

  | [NOT] DETERMINISTIC

  | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }

  | SQL SECURITY { DEFINER | INVOKER }

  | COMMENT 'string'

routine_body:

    procedimientos almacenados o comandos SQL válidos

19.2.2. ALTER PROCEDURE y ALTER FUNCTION

ALTER {PROCEDURE | FUNCTION} sp_name [characteristic ...]

characteristic:

    { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }

  | SQL SECURITY { DEFINER | INVOKER }

  | COMMENT 'string'

19.2.3. DROP PROCEDURE y DROP FUNCTION

DROP {PROCEDURE | FUNCTION} [IF EXISTS] sp_name

19.2.9.1. Declarar variables locales con DECLARE

DECLARE var_name[,...] type [DEFAULT value]

19.2.12.1. Sentencia IF

IF search_condition THEN statement_list

    [ELSEIF search_condition THEN statement_list] ...

    [ELSE statement_list]

END IF

EJEMPLO: Función calcular_importe_pedido

Diseña una función que, partiendo de un código de pedido, obtenga el importe de ese pedido, teniendo en cuenta la cantidad pedida, el precio y el IVA correspondiente (tipo general). El cálculo deberá tener en cuenta que haya existencias del artículo solicitado.

drop function if exist calcular_importe_pedido;

delimiter//

create function calcular_importe_pedido (codpedido INT)

returns float (8,2)

comment 'A partir de un codpedido que me devuelva el importe con iva.'

begin

        declare importe float (8,2);

        declare total_existencia int;

        declare producto varchar(10);

        declare cantidad_pedida int;

        select Idproducto, cantidad into producto, cantidad_pedida from pedidos where         idpedido=codpedido;

select sum(unidades) into total_existencia from existencias where IdProducto=producto;

if(total_existencia>=cantidad_pedida)

then

select cantidad*precio*1.21 into importe

from pedidos pe, productos p

where pe.IdProducto = p.IdProducto

and pe.IdPedido=codpedido;

else

select (total_existencia*precio)*1.21 into importe

from pedidos pe, productos p

where pe.IdProducto = p.IdProducto

and pe.IdPedido=codpedido;

end if;

return importe;

end //

delimiter;

SELECT calcular_importe_pedido(1);


19.2.12.2. La sentencia CASE

CASE case_value

    WHEN when_value THEN statement_list

    [WHEN when_value THEN statement_list] ...

    [ELSE statement_list]

END CASE

o:

CASE

    WHEN search_condition THEN statement_list

    [WHEN search_condition THEN statement_list] ...

    [ELSE statement_list]

END CASE

19.2.12.3. Sentencia LOOP

[begin_label:] LOOP

    statement_list

END LOOP [end_label]

19.2.12.4. Sentencia LEAVE

LEAVE label

19.2.12.5. La sentencia ITERATE

ITERATE label

19.2.12.6. Sentencia REPEAT

[begin_label:] REPEAT

    statement_list

UNTIL search_condition

END REPEAT [end_label]

19.2.12.7. Sentencia WHILE

[begin_label:] WHILE search_condition DO

    statement_list

END WHILE [end_label]

EJEMPLPO: trigger insertar_pedidos_clientes

Crea un disparador (trigger) que cada vez que se inserte un nuevo pedido en la tabla pedidos se realice una de las dos acciones siguientes:

  1. Si el cliente ya está grabado en la tabla pedidos_clientes, actualizar el importe acumulado de sus pedidos.
  2. Si el cliente no está grabado en la tabla pedidos_clientes, añadir el importe de su pedido.

DELIMITER|

DROP TRIGGER IF EXISTS insertar_pedidos_clientes|

CREATE TRIGGER insertar_pedidos_clientes BEFORE INSERT ON pedidos FOR EACH ROW

BEGIN

DECLARE n VARCHAR(45);

declare p float(6,2);

Select nombre into n from clientes where new.dni_cli=clientes.dni_cli;

select precio into p from productos where new.idproducto=productos.idproducto;

IF new.dni_cli in ( select dni_cliente from pedidos_clientes)

THEN

update pedidos_clientes

set importe_pedidos = importe_pedidos + (new.cantidad * p),

numero_pedidos = numero_pedidos+1

...

Descargar como (para miembros actualizados)  txt (9 Kb)   pdf (237.9 Kb)   docx (225.8 Kb)  
Leer 5 páginas más »
Disponible sólo en Clubensayos.com