Advertisement
lukifrancuz

BD3_K2_Z2_g1

Jan 19th, 2023
2,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.20 KB | None | 0 0
  1. --Zadanie 2
  2. --i ogólnie te CREATE, ON, AS, BEGIN i inne można z dużych napisać wtedy będzie inaczej wyglądać
  3. create trigger t_prod_update
  4. on Production.Products
  5. after update
  6. as
  7. begin
  8.     if @@rowcount=0 return
  9.     set nocount on
  10.    
  11.     declare @productid int;
  12.     set @productid = (select productid from deleted);
  13.        
  14.     if ((select productname from inserted) != (select productname from deleted))
  15.     begin
  16.     insert into Historia(tabela, operacja, recordid, oldvalue, newvalue, dataoperacji)
  17.         values ('Products', 'update', @productid, (select productname from deleted), (select productname from inserted), getdate());
  18.     end
  19.  
  20.     if ((select supplierid from inserted) != (select supplierid from deleted))
  21.     begin
  22.     insert into Historia(tabela, operacja, recordid, oldvalue, newvalue, dataoperacji)
  23.         values ('Products', 'update', @productid, (select supplierid from deleted), (select supplierid from inserted), getdate());
  24.     end
  25.  
  26.     if ((select categoryid from inserted) != (select categoryid from deleted))
  27.     begin
  28.     insert into Historia(tabela, operacja, recordid, oldvalue, newvalue, dataoperacji)
  29.         values ('Products', 'update', @productid, (select categoryid from deleted), (select categoryid from inserted), getdate());
  30.     end
  31.  
  32.     if ((select unitprice from inserted) != (select unitprice from deleted))
  33.     begin
  34.     insert into Historia(tabela, operacja, recordid, oldvalue, newvalue, dataoperacji)
  35.         values ('Products', 'update', @productid, (select unitprice from deleted), (select unitprice from inserted), getdate());
  36.     end
  37.  
  38.     if ((select discontinued from inserted) != (select discontinued from deleted))
  39.     begin
  40.     insert into Historia(tabela, operacja, recordid, oldvalue, newvalue, dataoperacji)
  41.         values ('Products', 'update', @productid, (select discontinued from deleted), (select discontinued from inserted), getdate());
  42.     end
  43. end
  44. go
  45.  
  46.  
  47. update Production.Products set productname = 'test PRODUKT', unitprice = 21.09 where productid = 3 -- tu inne wartości na testa i inne produkt id
  48. select * from Historia
  49. select * from Production.Products where productid = 3 -- tu inne produkt id (takie jak wyzej)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement