Advertisement
Ladies_Man

Update Trigger: w/ cursor, w/o cursor

Dec 14th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.99 KB | None | 0 0
  1. use lab9;
  2. go
  3.  
  4. --==============================================================
  5. -------------------------UPDATE-ON-VIEW-------------------------
  6. --==============================================================
  7. if OBJECT_ID(N'dbo.joined_trigger_update', N'TR') is not null
  8.     drop trigger dbo.joined_trigger_update
  9. go
  10. create trigger dbo.joined_trigger_update
  11.     on dbo.joined_view
  12.     instead of update
  13.     as
  14.     begin
  15.         set nocount on;
  16.         declare @entry varchar(35)
  17.         declare @new_param varchar(35)
  18.  
  19.         if UPDATE(title) RAISERROR('[TRIGGER UPD]: "title" cant be modified', 16, 1)
  20.         if UPDATE(author) RAISERROR('[TRIGGER UPD]: "author" cant be modified', 16, 1)
  21.        
  22.         if UPDATE(year)
  23.         begin
  24.             create table temp (
  25.                 title       varchar(254),
  26.                 year_new    int)
  27.  
  28.             insert into temp select i.title, i.year from inserted as i
  29.            
  30.             declare curs cursor
  31.                 for select temp.title, temp.year_new
  32.                 from temp
  33.  
  34.             open curs
  35.             fetch next from curs into @entry, @new_param
  36.            
  37.             while (@@FETCH_STATUS = 0)
  38.             begin
  39.                 update dbo.books
  40.                     set books.year = @new_param
  41.                     where books.title = @entry
  42.                    
  43.                 fetch next from curs into @entry, @new_param
  44.             end
  45.            
  46.             close curs
  47.             deallocate curs
  48.             drop table temp
  49.         end
  50.  
  51.         if UPDATE(rating)
  52.         begin
  53.             create table temp (
  54.                 title       varchar(254),
  55.                 rating_new  int)
  56.  
  57.             insert into temp select i.title, i.rating from inserted as i
  58.            
  59.             declare curs cursor
  60.                 for select temp.title, temp.rating_new from temp
  61.  
  62.             open curs
  63.             fetch next from curs into @entry, @new_param
  64.            
  65.             while (@@FETCH_STATUS = 0)
  66.             begin
  67.                 update dbo.books
  68.                     set books.rating = @new_param
  69.                     where books.title = @entry
  70.                    
  71.                 fetch next from curs into @entry, @new_param
  72.             end
  73.            
  74.             close curs
  75.             deallocate curs
  76.             drop table temp
  77.         end
  78.     end
  79. go
  80.  
  81. --==============================================================
  82. -------------------------UPDATE-ON-VIEW-------------------------
  83. --==============================================================
  84. if OBJECT_ID(N'dbo.joined_trigger_update', N'TR') is not null
  85.     drop trigger dbo.joined_trigger_update
  86. go
  87. create trigger dbo.joined_trigger_update
  88.     on dbo.joined_view
  89.     instead of update
  90.     as
  91.     begin
  92.  
  93.         if UPDATE(title) or UPDATE(author)
  94.             RAISERROR('[UPD TRIGGER]: "title" and "author" cant be modified', 16, 1)
  95.  
  96.         if UPDATE(year) or UPDATE(rating)
  97.             update dbo.books
  98.                 set
  99.                     books.year = (select year from inserted where inserted.title = books.title),
  100.                     books.rating = (select rating from inserted where inserted.title = books.title)
  101.                 where books.title = (select title from inserted where inserted.title = books.title)
  102.  
  103.     end
  104. go
  105.  
  106.  
  107.  
  108. update dbo.joined_view
  109.     set rating = 451,
  110.         year = 451
  111.     where joined_view.author = 'raymond bradbury';
  112. update dbo.joined_view
  113.     set rating = 10
  114.     where joined_view.author = 'george martin';
  115. update dbo.joined_view
  116.     set year = 2045
  117.     where joined_view.title = 'a dream of spring';
  118. select * from dbo.joined_view
  119. select * from dbo.books
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement