Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. DROP VIEW IF EXISTS item
  2. DROP TABLE IF EXISTS item
  3. DROP TABLE IF EXISTS histprice
  4. DROP TABLE IF EXISTS itemWithinfo
  5.  
  6. -- old no history TABLE
  7. CREATE TABLE item (
  8. itemno int identity(1,1) primary key,
  9. itemname varchar(25),
  10. price decimal(7,2)
  11. )
  12.  
  13. INSERT INTO item values('pommes frites',17),('small burger',25),('checken nuggets',28),('hot wings',27)
  14.  
  15. -- new system with history
  16. CREATE TABLE itemWithinfo (
  17. itemno int identity(1,1) primary key,
  18. itemname varchar(25)
  19. )
  20.  
  21. CREATE TABLE histprice (
  22. itemno int foreign key references itemWithinfo,
  23. price decimal(7,2),
  24. fromdate datetime
  25. )
  26.  
  27. SET IDENTITY_INSERT itemWithinfo on
  28.  
  29. INSERT INTO itemWithinfo (itemno, itemname)
  30. SELECT itemno, itemname FROM item where itemname is not null
  31.  
  32. INSERT INTO histprice
  33. SELECT itemno, price, GETDATE() FROM item
  34.  
  35. SET IDENTITY_INSERT itemWithinfo OFF
  36. select * from itemWithinfo
  37. select * from item
  38.  
  39. drop table item
  40. GO
  41.  
  42. CREATE VIEW item AS
  43. SELECT iwi.itemno, iwi.itemname, hp.price
  44. FROM histprice hp, itemWithinfo iwi
  45. WHERE iwi.itemno = hp.itemno
  46. GO
  47.  
  48. CREATE TRIGGER insertItem ON item
  49. INSTEAD OF INSERT AS
  50. BEGIN
  51. INSERT INTO itemWithInfo(itemname) SELECT itemname FROM inserted
  52. INSERT INTO histprice SELECT @@IDENTITY, price, GETDATE() FROM inserted
  53. END
  54. GO
  55.  
  56. alter TRIGGER updateItem ON item
  57. INSTEAD OF UPDATE AS
  58. BEGIN
  59. UPDATE itemWithInfo SET itemname = (SELECT itemname FROM inserted) WHERE itemWithInfo.itemno = (SELECT itemno from deleted)
  60. --UPDATE histprice SET price = (select price from inserted) WHERE histprice.itemno = (SELECT itemno from deleted)
  61. INSERT INTO histprice SELECT @@IDENTITY, price, GETDATE() FROM inserted
  62. END
  63. GO
  64.  
  65. CREATE TRIGGER deleteItem ON item
  66. INSTEAD OF DELETE AS
  67. BEGIN
  68. DELETE histprice WHERE itemno = (SELECT itemno FROM deleted)
  69. DELETE itemWithinfo WHERE itemno = (SELECT itemno FROM deleted)
  70. END
  71. GO
  72.  
  73. -- queries used by the old system
  74. SELECT price from item where itemno = 2
  75. INSERT INTO item(itemname,price) values ('big burger',45)
  76. update item set price = 42 where itemno = 1
  77. delete from item where itemno = 3
  78.  
  79. select * from itemWithInfo join histprice on histprice.itemno = itemWithinfo.itemno
  80. select * from histprice
  81. select * from item
  82.  
  83. select price from item where itemno = 2
  84. insert into item(itemname,price) values('big burger',45)
  85. update item set price = 50 where itemno = 4
  86. delete from item where itemno = 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement