Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.42 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_inventory
  7.     WITH EXECUTE AS CALLER
  8.     FOR UPDATE
  9.     AS
  10.     BEGIN
  11.        
  12.         declare
  13.             @all float,
  14.             @not_empty float,
  15.             @i float;  
  16.  
  17.         set @all = (
  18.             select count(distinct location)
  19.             from location
  20.             where location_template != 'DOCK' and location like '[^-]%'
  21.         );
  22.        
  23.         set @not_empty = (         
  24.             select count(distinct l.location)
  25.             from location l
  26.             where
  27.                 location_template != 'DOCK'
  28.                 and not exists (
  29.                     select distinct li.location
  30.                     from location_inventory li
  31.                     where
  32.                         li.location = l.location
  33.                         and li.location like '[^-]%'
  34.                         and (ON_HAND_QTY != 0.00000 or IN_TRANSIT_QTY != 0.00000
  35.                             or ALLOCATED_QTY != 0.00000 or SUSPENSE_QTY != 0.00000)
  36.                 )
  37.         );
  38.  
  39.        
  40.         set @i = ( cast(@not_empty as float) / cast(@all as float) ) * 100;
  41.         if @i !> 20.00 -- {
  42.             exec msdb.dbo.sp_send_dbmail
  43.                 @profile_name = 'Lestat',
  44.                 @recipients = 'AChmyrev@korusconsulting.ru',  
  45.                 @body = N'На складе осталось мене 20% пустых ячеек для хранения',  
  46.                 @subject = 'Automated Message' ;
  47.         --}
  48.  
  49.         set @i = null
  50.  
  51.     END
  52.    
  53.  
  54.     --select 1, TEMPLATE_FIELD1,* from LOCATION_INVENTORY where location like '-%'
  55.     --union
  56.     --select 2, TEMPLATE_FIELD1,* from LOCATION_INVENTORY where location not like '-%'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement