Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. USE [ercproxy]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[spa_KSESendCommand]    Script Date: 01/26/2015 17:40:33 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. /*
  9.         Procedure for sending command to KSEs
  10.        
  11.         Created:    2014-08-12
  12.         Creator:    Bartash
  13. */
  14. ALTER PROCEDURE [dbo].[spa_KSESendCommand]
  15.         @Command        INT             ,           -- integer value of command
  16.                                                     --      20 - Go out of service
  17.                                                     --      21 - Go in service
  18.                                                     --      24 - Reload streets
  19.                                                    
  20.         @TerminalId     INT     = NULL,             -- if NULL - All M-boxes
  21.         @ForceIN        INT     = 1                 -- Force to update state
  22. AS
  23. BEGIN
  24. --SET NOCOUNT ON
  25.  
  26. IF      object_id('tempdb..#KSECommandList') IS NOT NULL
  27.     DROP TABLE  #KSECommandList
  28.    
  29. CREATE TABLE    #KSECommandList (
  30.                 TerminalId      INT)
  31.  
  32. IF  @TerminalId IS NULL                             -- All terminals
  33.     INSERT INTO     #KSECommandList (TerminalId)
  34.         SELECT      Id
  35.         FROM        dbo.Shops
  36.         WHERE       Id > 16777000
  37.         AND         Id < 16777999
  38.         AND         STATUS = 1
  39. ELSE                                                -- Only one terminal
  40.     INSERT INTO     #KSECommandList (TerminalId)
  41.         SELECT      @TerminalId
  42.  
  43. DECLARE     @TermId     INT                         -- For terminal Id
  44.  
  45. DECLARE     curTerm     CURSOR FOR
  46.     SELECT  TerminalId
  47.     FROM    #KSECommandList
  48.  
  49. OPEN    curTerm
  50.  
  51. FETCH NEXT FROM curTerm INTO @TermId
  52.  
  53. DECLARE     @State      VARCHAR(60)
  54.  
  55. WHILE @@FETCH_STATUS = 0
  56. BEGIN
  57.     IF  @ForceIN = 0    -- Do not force to go IN!
  58.     BEGIN
  59.     SELECT      @State = Info
  60.     FROM        KSEStates
  61.     WHERE       Updated IN (
  62.                             SELECT      MAX(Updated)
  63.                             FROM        KSEStates
  64.                             WHERE       TerminalId  = @TermId
  65.                             AND         StatusId    = 3)
  66.             AND TerminalId = @TermId  -- типо наверное вот так нужно
  67.             AND StatusId = 3
  68.  
  69.         IF @@ROWCOUNT <> 1  -- [2015-01-26][Bartash] We need to get only one row in here!
  70.     BEGIN
  71.         RAISERROR('More than one row of State detected!', 5,1)
  72.         GOTO FINAL
  73.     END
  74.  
  75.     IF @State = 1       -- It went OUT!
  76.         GOTO NEXT_STEP
  77.     END
  78.  
  79.  
  80.     INSERT INTO ##KSECommandsRT (TerminalId, CommandId, Command, Updated)
  81.     VALUES (@TermId, @Command, 0, getdate())
  82.  
  83. NEXT_STEP:
  84.     FETCH NEXT FROM curTerm INTO @TermId
  85. END
  86.  
  87. FINAL:
  88.  
  89. CLOSE curTerm
  90. DEALLOCATE curTerm
  91.  
  92. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement