Advertisement
lukifrancuz

BD3_K2_Z1_g1

Jan 19th, 2023
1,858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.46 KB | None | 0 0
  1. use TSQL2012
  2.  
  3. create table Historia(
  4.     id int identity(1,1) not null,
  5.     tabela varchar(20) not null,
  6.     operacja varchar(20) not null,
  7.     recordid int not null,
  8.     oldvalue varchar(100),
  9.     newvalue varchar(100),
  10.     dataoperacji date not null
  11.     )
  12. go
  13.  
  14. --Zadanie 1
  15. create trigger t_prod_delete
  16. on Production.Products
  17. instead of delete
  18. as
  19. begin
  20.     alter table Production.Products nocheck constraint all
  21.     alter table Sales.OrderDetails nocheck constraint all
  22.  
  23.     declare @productid int;
  24.     set @productid = (select productid from deleted);
  25.  
  26.     delete from Sales.Orders where orderid in (select orderid from Sales.OrderDetails where productid = @productid);
  27.     delete from Sales.OrderDetails where productid = @productid;
  28.     delete from Production.Products where productid = @productid;
  29.  
  30.     insert into Historia(tabela, operacja, recordid, oldvalue, newvalue, dataoperacji)
  31.         values ('Products', 'delete', @productid, null, null, getdate());
  32.  
  33.  
  34.     alter table Production.Products check constraint all
  35.     alter table Sales.OrderDetails check constraint all
  36. end
  37. go
  38.  
  39. --To jest do testów, ustawiasz inne niż 2 i testujesz, na początku będzie coś a jak skasujesz to już nic ^^
  40. select * from Production.Products where productid = 2
  41. select * from Sales.OrderDetails where productid = 2
  42.  
  43. delete from Production.Products where productid = 2
  44.  
  45. select * from Production.Products where productid = 2
  46. select * from Sales.OrderDetails where productid = 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement