Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.94 KB | None | 0 0
  1. USE [SUPFD_TEST]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[usp_RemoveEquipmentOperation]    Script Date: 2/23/2017 11:40:18 AM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. -- =============================================
  10. -- Author:      mgonzale
  11. -- Create date: 2016-25-10
  12. -- Description: Removes Equipment Operations
  13. --              cramirez 02.21.2017 When there's records on collect_schemas_operations_equipments and algo on schemas_operations_equipments it only updates the records to status= 'INA',
  14. --                                  When there's not any record on collect that's when it deletes all assignments and all relations between the operation and the equipment
  15. --              cramirez 02.21.2017 added parameter for user name when a record is set to ina
  16. -- =============================================
  17.  
  18. EXEC dbo.usp_RemoveEquipmentOperation 'L24B', '055', 'P32', 'cramirez'
  19.  
  20. ALTER PROCEDURE [dbo].[usp_RemoveEquipmentOperation]
  21.     @Equipment NVARCHAR(15)
  22.    ,@AddedOperation NVARCHAR(500)
  23.    ,@siteKey NVARCHAR(10)
  24.    ,@USER NVARCHAR(50)
  25. AS
  26.  
  27. DECLARE @DATE SMALLDATETIME = GETDATE()
  28.  
  29. BEGIN
  30.     IF EXISTS (SELECT TOP 1 1 FROM dbo.collect_schemas_operations_equipments csoe WHERE csoe.scoe_equi_equipment_key = @Equipment AND csoe.opre_operation_key = @AddedOperation AND csoe.scoe_site_key = @siteKey AND csoe.record_status = 'ACT')
  31.     BEGIN
  32.         IF EXISTS (SELECT 1 FROM operations_equipments WHERE equi_equipment_key=@Equipment AND oper_operation_key  = @AddedOperation AND equi_site_key=@siteKey AND [STATUS] = 'ACT')
  33.             IF EXISTS (SELECT 1 FROM dbo.schemas_operations_equipments soe WHERE soe.opeq_equi_equipment_key = @Equipment AND soe.opeq_operation_key = @AddedOperation AND soe.record_status = 'ACT')
  34.             BEGIN
  35.        
  36.                 UPDATE dbo.schemas_operations_equipments
  37.                 SET record_status = 'INA', modified_by = @USER, modified_on = @DATE
  38.                 WHERE opeq_operation_key = @AddedOperation AND opeq_equi_equipment_key = @Equipment AND opeq_site_key = @siteKey AND opeq_equi_site_key = @siteKey AND whst_site_key = @siteKey
  39.  
  40.                 UPDATE  operations_Equipments
  41.                 SET [STATUS] = 'INA', modified_by = @USER, modified_on = @DATE
  42.                 WHERE
  43.                 equi_equipment_key=@Equipment AND oper_operation_key  = @AddedOperation AND oper_site_key = @siteKey AND equi_site_key = @siteKey  
  44.             END
  45.             ELSE
  46.             BEGIN
  47.                
  48.                 /**DELETE RATES**/
  49.                 DELETE FROM dbo.operations_equipments_wheel_styles_rates
  50.                 WHERE opeq_equi_equipment_key = @equipment AND opeq_operation_key = @AddedOperation AND opeq_equi_site_key = @siteKey
  51.  
  52.                 DELETE FROM dbo.operations_equipments WHERE
  53.                 equi_equipment_key = @Equipment AND oper_operation_key = @AddedOperation AND oper_site_key = @siteKey AND equi_site_key = @siteKey  
  54.             END
  55.     END
  56.     ELSE
  57.     BEGIN
  58.         IF EXISTS (SELECT 1 FROM operations_equipments WHERE equi_equipment_key=@Equipment AND oper_operation_key  = @AddedOperation AND equi_site_key=@siteKey AND [STATUS] = 'ACT')
  59.         BEGIN
  60.             IF EXISTS (SELECT 1 FROM dbo.schemas_operations_equipments soe WHERE soe.opeq_equi_equipment_key = @Equipment AND soe.opeq_operation_key = @AddedOperation AND soe.record_status = 'ACT')
  61.             BEGIN
  62.                 DELETE FROM dbo.schemas_operations_equipments
  63.                 WHERE opeq_operation_key = @AddedOperation AND opeq_equi_equipment_key = @Equipment AND opeq_site_key = @siteKey AND opeq_equi_site_key = @siteKey AND whst_site_key = @siteKey
  64.  
  65.                 /**DELETE RATES**/
  66.                 DELETE FROM dbo.operations_equipments_wheel_styles_rates
  67.                 WHERE opeq_equi_equipment_key = @equipment AND opeq_operation_key = @AddedOperation AND opeq_equi_site_key = @siteKey
  68.  
  69.                 DELETE FROM operations_Equipments
  70.                 WHERE equi_equipment_key=@Equipment AND oper_operation_key  = @AddedOperation AND oper_site_key = @siteKey AND equi_site_key = @siteKey  
  71.             END
  72.             ELSE
  73.             BEGIN
  74.                 DELETE FROM operations_Equipments
  75.                 WHERE equi_equipment_key=@Equipment AND oper_operation_key  = @AddedOperation AND oper_site_key = @siteKey AND equi_site_key = @siteKey  
  76.             END
  77.         END
  78.     END  
  79. END
  80.  
  81. SELECT * FROM dbo.operations_equipments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement