You are on page 1of 8

UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN

FACULTAD DE INGENERIA
ESCUELA DE SISTEMAS Y COMPUTACIN - VIII SEMESTRE

SISTEMAS DE BASES II
TEMA:
FUNCIONES Y CONSULTAS EN
SQL
DOCENTE:
Ing. MUOZ ROBLES, Williams
Antonio
ALUMNA:
PALMA CRISTOBAL, Tania

CERRO DE PASCO 2016

FUNCIONES Y CONSULTAS EN
SQL SERVER
CONSULTA Y FUNSION (1)
CONSULTAS

Sistemas de Bases II

Pgina 2

SELECT FACTURA.SERFAC, FACTURA.NROFAC, CLIENTE.NOMBRE, SUM


(STOT) AS MONTO
FROM FACTURA INNER JOIN DFACTURA ON
FACTURA.SERFAC=DFACTURA.SERFAC AND
FACTURA.NROFAC=DFACTURA.NROFAC INNER JOIN CLIENTE
ON FACTURA.CODCLI=CLIENTE.CODCLI
GROUP BY FACTURA.SERFAC, FACTURA.NROFAC, CLIENTE.NOMBRE
FUNCIN

CREATE FUNCTION [DBO]. [MONTO] (@S INT,@N INT)


RETURNS NUMERIC (3,0)
AS
BEGIN
DECLARE @R NUMERIC (3,0)
SELECT @R=SUM (STOT)
FROM DFACTURA
WHERE SERFAC=@S AND NROFAC=@N
RETURN(ISNULL(@R,0))
END
SELECT SERFAC, NROFAC, NOMBRE, DBO.MONTO (SERFAC, NROFAC)
AS MONTO
FROM CLIENTE INNER JOIN FACTURA ON
CLIENTE.CODCLI=FACTURA.CODCLI

TRIGGER
CREATE TRIGGER [DBO]. [TR_DFACTURA]
ON [DBO]. [DFACTURA]
FOR INSERT, UPDATE
AS
BEGIN
DECLARE @S INT
DECLARE @N INT
SELECT @S=SERFAC, @N=NROFAC
FROM INSERTED
DECLARE @TOT INT
Sistemas de Bases II

Pgina 3

SELECT @TOT=SUM (STOT)


FROM DFACTURA
WHERE SERFAC=@S AND NROFAC=@N
UPDATE FACTURA
SET TOTAL=ISNULL(@TOT,0)
WHERE SERFAC=@S AND NROFAC=@N
END

CONSULTA Y FUNSION (2)


CONSULTAS
SELECT CUSTOMER.CUSTOMER_ID, CUSTOMER.FIRST_NAME,
CUSTOMER.LAST_NAME, SUM (PAYMENT.AMOUNT) AS MONTO
FROM CUSTOMER INNER JOIN PAYMENT ON
CUSTOMER.CUSTOMER_ID=PAYMENT.CUSTOMER_ID
GROUP BY CUSTOMER.CUSTOMER_ID, CUSTOMER.FIRST_NAME,
CUSTOMER.LAST_NAME
ORDER BY MONTO
FUNCIN
CRETATE FUNCTION [DBO].[SUM_MONTO](@S NUMERIC (3,0))
RETURNS NUMERIC (10,2)
AS
BEGIN
DECLARE @TOT NUMERIC (10,2)
SELECT @TOT=SUM (AMOUNT)
FROM PAYMENT
WHERE CUSTOMER_ID=@S
RETURN(ISNULL(@TOT,0))
END
SELECT CUSTOMER_ID, FIRST_NAME, LAST_NAME, DBO.SUM_MONTO
(CUSTOMER_ID) AS MONTO
FROM CUSTOMER
ORDER BY MONTO

Sistemas de Bases II

Pgina 4

CONSULTA

FUNSION

(3)

CONSULTAS
SELECT FILM.FILM_ID, TITLE, COUNT (FILM_ACTOR.ACTOR_ID) AS
CANT_ACTORES
FROM FILM_ACTOR INNER JOIN FILM ON FILM_ACTOR.FILM_ID =
FILM.FILM_ID
GROUP BY FILM.FILM_ID, TITLE
ORDER BY TITLE
FUNCIN
CREATE FUNCTION [DBO].[ACTOR_PELICULA](@S VARCHAR (5))
RETURNS INT
AS
BEGIN
DECLARE @C INT
SELECT @C=COUNT (ACTOR_ID)
FROM FILM_ACTOR
WHERE FILM_ID=@S
RETURN(ISNULL(@C,0))
END
SELECT FILM_ID, TITLE, DBO.ACTOR_PELICULA (FILM_ID)
FROM FILM
ORDER BY TITLE

CONSULTA Y FUNSION (4)

Sistemas de Bases II

Pgina 5

CONSULTAS
SELECT CATEGORY.CATEGORY_ID, CATEGORY.NAME, COUNT
(FILM.FILM_ID) AS PELICULAS
FROM FILM INNER JOIN FILM_CATEGORY ON FILM.FILM_ID =
FILM_CATEGORY.FILM_ID INNER JOIN CATEGORY ON
FILM_CATEGORY.CATEGORY_ID=CATEGORY.CATEGORY_ID
GROUP BY CATEGORY. CATEGORY_ID , CATEGORY.NAME
ORDER BY NAME
FUNCIN
CREATE FUNCTION [DBO].[CATEGORIA_PELICULA](@S INT)
RETURNS INT
AS
BEGIN
DECLARE @C INT
SELECT @C=COUNT (FILM_ID)
FROM FILM_CATEGORY
WHERE CATEGORY_ID=@S
RETURN(ISNULL(@C,0))
END
SELECT CATEGORY_ID, NAME, DBO.CATEGORIA_ PELICULA
(CATEGORY_ID) AS PELICULAS
FROM CATEGORY
ORDER BY NAME

Sistemas de Bases II

Pgina 6

CONSULTA

FUNSION

(5)

CONSULTAS
SELECT COUNTRY.COUNTRY_ID, COUNTRY.COUNTRY, COUNT
(CUSTOMER.CUSTOMER_ID) AS CLIENTES
FROM CUSTOMER INNER JOIN T_ADDRESS ON
CUSTOMER.ADDRESS_ID=T_ADDRESS.ADDRESS_ID
INNER JOIN CITY ON T_ADDRESS.CITY_ID=CITY.CITY_ID INNER JOIN
COUNTRY ON CITY.COUNTRY_ID=COUNTRY.COUNTRY_ID
GROUP BY COUNTRY.COUNTRY_ID, COUNTRY.COUNTRY
ORDER BY COUNTRY
FUNCIN
CREATE FUNCTION [DBO].[PAIS_CLIENTE](@S INT)
RETURNS INT
AS
BEGIN
DECLARE @C INT
SELECT @C=COUNT (CUSTOMER.CUSTOMER_ID)
FROM CUSTOMER INNER JOIN T_ADDRESS ON
CUSTOMER.ADDRESS_ID=T_ADDRESS.ADDRESS_ID
INNER JOIN CITY ON T_ADDRESS.CITY_ID=CITY.CITY_ID INNER JOIN
COUNTRY ON CITY.COUNTRY_ID=COUNTRY.COUNTRY_ID
WHERE COUNTRY.COUNTRY_ID=@S
RETURN(ISNULL(@C,0))
END
SELECT COUNTRY_ID, COUNTRY, DBO.PAIS_CLIENTE (COUNTRY_ID) AS
CLIENTES
FROM COUNTRY
ORDER BY COUNTRY

Sistemas de Bases II

Pgina 7

Sistemas de Bases II

Pgina 8