You are on page 1of 4

Baze te dhenash Arinela Anamali Dt 21/04/2020

Laborator 4
1. Krijoni nje funksion qe merr si parameter vitin , shumen e totalit te shitjeve dhe si
rezultat kthen nje tabele e cila ka fushat emri i produktit, kategorine e produktit, emrin
e kompanise, id e klientit dhe totalin e shitjeve per vitin qe vendosim si parameter
@year dhe per shuma me te medha se parametri @amount.

CREATE FUNCTION dbo.udf_top_customers_and_reps(


@year INT,
@amount INT)
RETURNS
@temp TABLE(
ProductName VARCHAR(200),
CategoryName VARCHAR(200),
CustomerID CHAR(5),
CompanyName VARCHAR(200),
TotalSales INT
)
AS
BEGIN

Return(

INSERT @temp(
ProductName ,
CategoryName ,
CompanyName ,
CustomerID ,
TotalSales
)

SELECT
ProductName,
CategoryName,
CompanyName,
b.CustomerID,
SUM(a.UnitPrice * quantity) AS total_sales
FROM [order details] a
INNER JOIN orders b ON a.orderid = b.orderid
INNER JOIN products c ON c.productid = a.productid
INNER JOIN customers d ON d.customerid = b.customerid
INNER JOIN categories e ON e.CategoryID = c.CategoryID
WHERE DATEPART(YEAR, OrderDate)= @ year
GROUP BY c.ProductName, e.CategoryName, b.CustomerID,
d.CompanyName,DATEPART(YEAR, OrderDate)
HAVING SUM(a.UnitPrice * quantity)> @ amount
ORDER BY ProductName
)
END

2. Krijoni nje sp ku te beni update sasine per porosi te dates me te madhe se '1996-01-01'

Create PROCEDURE Update_Quantity


(@date datetime)

1
Baze te dhenash Arinela Anamali Dt 21/04/2020

as

BEGIN
update [Order Details]
set quantity = 10
where exists (SELECT o.OrderDate
FROM dbo.Orders o INNER JOIN
dbo.[Order Details] od ON o.OrderID = od.OrderID
where OrderDate > @date)

END

3. Krijoni nje triger qe pasi behet insert nje porosi te modifkohet sasia tek fusha
UnitsInStock dhe UnitsInOrder
CREATE TRIGGER trgOrderInventoryInsert
ON dbo.[Order Details]
AFTER INSERT
AS
UPDATE P SET
UnitsInStock =
UnitsInStock - I.IQuantity,
UnitsOnOrder =
UnitsOnOrder + I.IQuantity
FROM Products AS P
INNER JOIN
(SELECT ProductID,
SUM(Quantity) As IQuantity
FROM inserted
GROUP BY ProductID) AS I
ON P.ProductID = I.ProductID
SET NOCOUNT OFF

4. Krijoni nje triger qe pasi behet Delete nje porosi te modifkohet sasia fusha
UnitsInStock dhe UnitsInOrder
CREATE TRIGGER trgOrderInventoryDelete
ON dbo.[Order Details]
AFTER DELETE
AS
UPDATE P SET
UnitsInStock =
UnitsInStock + D.DQuantity,
UnitsOnOrder =
UnitsOnOrder - D.DQuantity
FROM Products AS P
INNER JOIN
(SELECT ProductID,
SUM(Quantity) As DQuantity
FROM deleted
GROUP BY ProductID) AS D
ON P.ProductID = D.ProductID
SET NOCOUNT OFF

2
Baze te dhenash Arinela Anamali Dt 21/04/2020

5. Krijoni nje trigger ku pasi shtohet nje porosi te behet kontrolli nqs sasia e porositur
eshte me e madhe se ajo ne stock atehere nuk mund te behet insert porosia dhe te
shfaqet sms ‘Nuk ka stock te mjaftueshem per te bere kete porosi’.
Create TRIGGER trgOrderDetailsValidate
ON dbo.[Order Details]
AFTER INSERT
AS
IF EXISTS(SELECT *
FROM Products AS P
INNER JOIN
(SELECT ProductID, SUM(Quantity) As IQuantity
FROM inserted
GROUP BY ProductID) AS I
ON P.ProductID = I.ProductID
WHERE I.IQuantity > P.UnitsInStock)
BEGIN
ROLLBACK TRAN
RAISERROR('Nuk ka stock te mjaftueshem per te bere kete porosi', 16, 1)

END

6. Krijoni nje triger qe nqs tentohet fshirja e nje rreshti nga tabela Categories do shfaqet
mesazhi ‘Nuk lejohet te fshihet nga tabela Categories’

CREATE TRIGGER NoCategoriesDelete ON dbo.Categories


INSTEAD OF DELETE
AS
BEGIN
DECLARE @Count INT

SET @Count = @@ROWCOUNT


IF @Count == 0
RETURN

BEGIN
RAISERROR
(N' Nuk lejohet te fshihet nga tabela Categories', -- Message
10, -- Severity.
1) -- State.

-- Rollback
IF @@error > 0
BEGIN
ROLLBACK TRANSACTION
END
END
END

7. Krijoni nje trigger ku nuk lejon te shtohet nje record ne tabelen Region nqs fusha
RegionDescription ka me pak se 7 karaktere.
CREATE TRIGGER RegionLength
ON dbo.Region

3
Baze te dhenash Arinela Anamali Dt 21/04/2020

AFTER INSERT
AS
BEGIN
DECLARE @RegionDescription nvarchar(50)
SELECT @RegionDescription = RegionDescription
FROM inserted
IF len(@RegionDescription) < 7
BEGIN
ROLLBACK TRANSACTION
END
END

You might also like