Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.54 KB | None | 0 0
  1. USE [interslisko5]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[csp_afterupdate]    Script Date: 2016-08-24 10:34:37 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. -- This procedure is run after each add/update/delete of data in LIME.
  10. -- IMPORTANT: Always avoid cursors if possible!
  11. -- Version 5.2.0
  12.  
  13. ALTER PROCEDURE [dbo].[csp_afterupdate]
  14.     @@sessionidentifier NVARCHAR(64) = N''
  15.     , @@iduser INT = NULL
  16.     , @@transactionid UNIQUEIDENTIFIER
  17. AS
  18. BEGIN
  19.    
  20.     -- Variables needed
  21.     DECLARE @recordcount INT
  22.     DECLARE @idrecord INT
  23.     DECLARE @NEW INT
  24.     DECLARE @updated INT
  25.     DECLARE @deleted INT
  26.    
  27.     -- Table to hold data from the updatelog table
  28.       DECLARE @temptable TABLE
  29.     (
  30.         [TABLE] nvarchar(64),
  31.         [idrecord] INT,
  32.         [NEW] INT,
  33.         [updated] INT,
  34.         [deleted] INT,
  35.         [USER] INT
  36.     )
  37.    
  38.     -- Find data to work with
  39.     INSERT INTO @temptable([TABLE], [idrecord], [NEW], [updated], [deleted], [USER])
  40.     SELECT [TABLE], [idrecord], [NEW], [updated], [deleted], [iduser]
  41.     FROM updatelog ul
  42.         WHERE transactionid = @@transactionid
  43.         AND ul.[TABLE] IN ('deal')
  44.  
  45.  
  46. --  SELECT [table]
  47.     --  , [idrecord]
  48. --      , [new]
  49.     --  , [updated]
  50.     --  , [deleted]
  51. --      , [relation]
  52. --  FROM [updatelog]
  53. --  WHERE [transactionid] = @@transactionid
  54.  
  55.     -- Start doing stuff if we should
  56. --  SELECT @relevantrecordscount = COUNT(*)
  57. --  FROM @temptable
  58. --  WHERE [table] IN (N'company')       -- ##TODO: Add all tables that there is code for in this procedure
  59.  
  60. --  IF @relevantrecordscount > 0
  61.  
  62.  
  63.     BEGIN
  64.    
  65.         -- ##TODO: The line below can be removed when adding other code!
  66.         --         Just a place holder since there must be some piece of code between BEGIN and END.
  67.         IF EXISTS
  68.         (
  69.             SELECT 0 FROM @temptable WHERE [TABLE] = 'deal' AND ([NEW] = 1 OR [updated]=1)
  70.         )
  71.    
  72.             UPDATE c
  73.             SET revenueold =  (SELECT SUM(VALUE) FROM deal b INNER JOIN @temptable t ON  b.iddeal = t.[idrecord] INNER JOIN company c ON c.idcompany = b.company
  74.                                 WHERE company = c.idcompany AND b.quotesent >= DateAdd(YEAR, -2, GetDate()) AND b.quotesent < DateAdd(YEAR, -1, GetDate()) AND b.probability = 1)
  75.             , revenuenew = (SELECT SUM(VALUE) FROM deal b INNER JOIN @temptable t ON  b.iddeal = t.[idrecord] INNER JOIN company c ON c.idcompany = b.company
  76.                                 WHERE company = c.idcompany AND b.quotesent >= DateAdd(YEAR, -1, GetDate()) AND b.probability = 1)
  77.            
  78.             FROM company c
  79.             INNER JOIN deal b ON c.idcompany=b.company
  80.             INNER JOIN @temptable t ON b.iddeal= t.[idrecord]
  81.             WHERE t.[TABLE] = 'deal' AND (t.[NEW] = 1 OR t.[updated] =1)
  82.              AND c.[STATUS] = 0
  83.     END
  84. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement