Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.21 KB | None | 0 0
  1.  
  2. /*  Task from AChmyrev
  3.     less then 20 percent of empty location
  4. */
  5.  
  6. create trigger KC_LESS_THEN_20_PERCENT_OF_EMPTY_LOCATIONS on dbo.location
  7.     WITH EXECUTE AS CALLER
  8.     FOR UPDATE
  9.     AS
  10.     BEGIN
  11.        
  12.         declare @all float, @not_empty float, @i float;
  13.         set @all = (
  14.             select count(distinct location)
  15.             from location
  16.             where location_template != 'DOCK'
  17.         );
  18.         set @not_empty = (         
  19.             select count(distinct l.location)
  20.             from location l
  21.             where
  22.                 location_template != 'DOCK'
  23.                 and not exists (
  24.                     select distinct li.location
  25.                     from location_inventory li
  26.                     where
  27.                         li.location = l.location
  28.                         and li.location != substring(li.location, 2, len(li.location))
  29.                         and (ON_HAND_QTY != 0.00000 or IN_TRANSIT_QTY != 0.00000
  30.                             or ALLOCATED_QTY != 0.00000 or SUSPENSE_QTY != 0.00000)
  31.                 )
  32.         );
  33.  
  34.        
  35.         set @i = ( cast(@not_empty as float) / cast(@all as float) ) * 100;
  36.         if @i !> 20.00
  37.             exec msdb.dbo.sp_send_dbmail
  38.                 @profile_name = 'Lestat',
  39.                 @recipients = 'AChmyrev@korusconsulting.ru',  
  40.                 @body = N'На складе осталось мене 20% пустых ячеек для хранения',  
  41.                 @subject = 'Automated Message' ;
  42.  
  43.         set @i = null
  44.  
  45.     END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement