Aromas de Base de datos y otras fragancias de programación

Page 95

Aromas de Bases de Datos y otras fragancias de Programación FUNCIONES SELECT CONCAT(LASTNAME, ' ', FIRSTNAME) AS EMPLEADO FROM EMPLOYEES drop function if exists ncompleto; create function ncompleto(id int) returns char(50) return (SELECT concat(lastname, ' ', firstname) from employees where employeeid=id); SELECT NCompleto(1) AS Empleado; SELECT NCompleto(7) AS Empleado; SELECT EmployeeID, NCompleto(EmployeeID) as Empleado FROM Employees;

/*CREAR UN FUNCION PARA OBTENER EL SUBTOTAL DE LAS VENTAS. USAR LA TABLA ORDERDETAILS STOTAL=UNIPRICE*QUANTITY*(1-DISCOUNT)*/ SELECT orderid, productid, unitprice, quantity,discount, unitprice*quantity*(1-discount) as st from orderdetails where orderid=10248; create function stotal1(unitprice numeric(10,2), quantity int, discount numeric(5,2)) returns numeric(10,2) return (unitprice*quantity*(1-discount)); create function stotal2(oid int, pid int) returns numeric(10,2) return ( SELECT unitprice*quantity*(1-discount) from orderdetails where orderid=oid and productid=pid);

/****PROBANDO *****/ SELECT orderid, productid, unitprice, quantity, discount, stotal1(unitprice,quantity,discount) st from orderdetails where orderid=10248; SELECT orderid, productid, unitprice, quantity,discount, stotal2(orderid,productid) st from orderdetails where orderid=10248;

/*USANDO LA FUNCION STOTAL. OBTENER EL PRODUCTO MAS RENTABLE*/ SELECT PRODUCTID, STOTAL2(ORDERID,PRODUCTID) ST FROM ORDERDETAILS ORDER BY ST DESC LIMIT 1; drop function if exists nproducto; create function nproducto(id int) returns char(50) return (SELECT productname from products where productid=id);

/*OBTENIENDO EL NOMBRE DEL PRODCUTO MAS RENTABLE*/ SELECT NPRODUCTO(PRODUCTID) PRODUCTO, STOTAL2(ORDERID,PRODUCTID) ST FROM ORDERDETAILS ORDER BY ST DESC LIMIT 1;

/*VER LAS INTRUCCIONES QUE PERMITIERON CREAR LA FUNCION*/ SHOW CREATE FUNCTION STOTAL2 \G

Página 95

Autor: Ing. Núñez Marinovich Néstor Manuel Ingeniero Informático

URL: http://manhiuco.es.tl Email: manhiuco@hotmail.com


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.