Advertisement
lukifrancuz

BD3_K1_Z2_g1

Nov 17th, 2022
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.90 KB | None | 0 0
  1. --2
  2.  
  3. create or alter procedure UpdateBestsellers
  4.     @kategoria varchar(10) = null,
  5.     @year int
  6. as
  7. begin
  8.  
  9.     if  not exists (select * from Production.Categories where categoryname = @kategoria)
  10.         insert into Production.Categories (categoryname, description)
  11.         values (@kategoria, '-----')
  12.    
  13.     declare @newCatId int
  14.     set @newCatId = (select top 1 categoryid from Production.Categories where categoryname = @kategoria)
  15.  
  16.     update Production.Products set categoryid = @newCatId where productid in (
  17.         select top 3 sod.productid from Sales.OrderDetails sod
  18.         inner join Sales.Orders so on sod.orderid = so.orderid
  19.         where year(so.orderdate) = @year
  20.         group by productid order by sum(qty) desc )
  21. end
  22. go
  23.  
  24. exec UpdateBestsellers @kategoria = 'Top2006', @year = 2006
  25.  
  26. select * from Production.Categories
  27. select * from Production.Products where categoryid = 13 -- tutaj zamiast 13 nowe id z Production.Categories
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement