Advertisement
EvgenyRudenko

Get_Type_Reason_Operation

Mar 7th, 2024
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.09 KB | None | 0 0
  1. USE [Frontol]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[Get_Type_Reason_Operation]    Script Date: 29.02.2024 9:44:29 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author:      NAS
  10. -- Department: VV
  11. -- Create date: 2022-03-02
  12. -- Description: процедура для получения типов и причин операций для запуска с касс
  13. -- <Last> 2024-01-31 NAS Новое логирование и приведедение к стандарту</Last>
  14.  
  15.  
  16. -- =============================================
  17. CREATE OR ALTER   PROCEDURE [dbo].[Get_Type_Reason_Operation]
  18.  
  19.   WITH EXECUTE AS OWNER
  20.  
  21. AS
  22. BEGIN
  23.   SET NOCOUNT ON;
  24.  
  25.   --/*--<Debug>
  26.   DECLARE
  27.     @DebugLog           TinyInt           = 1,  -- 1 Start/Finish -- 2 +Point -- 3 +SubPoint
  28.     @DebugLogResults    Bit               = 0,  -- Если планируется логирование результатов работы процедуры
  29.     @DebugPrint         TinyInt,                -- 1 Start/Finish -- 2 +Point -- 3 +SubPoint
  30.     @DebugSelect        TinyInt,                -- Mask(1) Start -- Mask(2) Point -- Mask(4) SubPoint -- Mask(8) Finish
  31.  
  32.     @DebugDbId          Int               = DB_ID(),
  33.     @DebugProcId        Int               = @@PROCID,
  34.     @DebugParams        [Debug].[Params],
  35.     @DebugError         NVarChar(1024),
  36.     @DebugErrorLine     Int,
  37.     @DebugErrorProc     NVarChar(512),
  38.     @DebugComment       NVarChar(256),
  39.     @DebugRowCount      Int,
  40.     @DebugContext       [Debug].[Context];
  41.   --*/--</Debug>
  42.  
  43.   BEGIN TRY
  44.     ------------------------------------------------------------------------
  45.     --/*--<Debug Start>
  46.     -- Если вы не хотите позволить управление логированием извне, то закомментите эту строчку
  47.     SET @DebugLog         = IsNull(CAST(SESSION_CONTEXT(N'DEBUG:LOG')     AS TinyInt), @DebugLog);
  48.     -- Управление принтовкой отладчика извне
  49.     SET @DebugPrint       = IsNull(CAST(SESSION_CONTEXT(N'DEBUG:PRINT')  AS TinyInt), 0);
  50.     -- Управление SELECT'ами отладчика извне
  51.     SET @DebugSelect      = IsNull(CAST(SESSION_CONTEXT(N'DEBUG:SELECT') AS TinyInt) & 0xF, 0);
  52.  
  53.     IF @DebugLog > 0 OR @DebugPrint > 0 OR @DebugSelect > 0 BEGIN
  54.       -- Все важные параметры процедуры должны быть переданы отладчику
  55.       SET @DebugParams = NULL;
  56.  
  57.       EXEC [Debug].[Execution@Start] @Db_Id = @DebugDbId, @Proc_Id = @DebugProcId, @Context = @DebugContext OUT, @Params = @DebugParams, @Log = @DebugLog, @Print = @DebugPrint, @Select = @DebugSelect;
  58.     END
  59.     --*/--</Debug Start>
  60.     --------------------------------------------------------------------
  61.    
  62.     DECLARE @json NVARCHAR(MAX) = N'[
  63.    {
  64.        "operation_code": 1,
  65.        "operation_name": "Дегустация",
  66.        "id": 2,
  67.        "name": "Обед"
  68.    },
  69.    {
  70.        "operation_code": 1,
  71.        "operation_name": "Дегустация",
  72.        "id": 3,
  73.        "name": "Хознужды"
  74.    }
  75. ]'
  76.  
  77. SELECT
  78.     [code_operation],
  79.     [name_operation],
  80.     [id_reason],
  81.     [name_reason]
  82. FROM OPENJSON(@json)
  83. WITH (
  84.     [code_operation] INT '$.operation_code',
  85.     [name_operation] NVARCHAR(100) '$.operation_name',
  86.     [id_reason] INT '$.id',
  87.     [name_reason] NVARCHAR(100) '$.name'
  88. )
  89.     /*
  90.     SELECT
  91.       [tpo].[code_operation],
  92.       [tpo].[name_operation],
  93.       ISNULL([ro].[id_reason], 0)    [id_reason],
  94.       ISNULL([ro].[name_reason], '') [name_reason]
  95.     FROM [dbo].[Types_Operation] AS [tpo] WITH (NOLOCK)
  96.     LEFT JOIN (
  97.                 SELECT
  98.                   [r].[code_operation],
  99.                   [r].[id_reason],
  100.                   [tt].[name_operation] [name_reason]
  101.                 FROM [dbo].[Reason_Operation]      AS [r] WITH (NOLOCK)
  102.                 INNER JOIN [dbo].[Types_Operation] AS [tt] WITH (NOLOCK) ON [tt].[code_operation]  = [r].[id_reason]
  103.                                                                         AND [tt].[table_operation] = [r].[table_operation]
  104.                 WHERE [tt].[field_operation] = 'id_reason'
  105.                   AND [tt].[table_operation] = 'td_move'
  106.                   AND [r].[table_operation]  = 'td_move'
  107.               )                  AS [ro] ON [ro].[code_operation] = [tpo].[code_operation]
  108.     WHERE [tpo].[field_operation] = 'operation_type_orig'
  109.       AND [tpo].[table_operation] = 'td_move'
  110.       AND NOT [tpo].[code_operation] IN (501, 500, 107, 117)
  111.       AND [tpo].[type_operation] IN (1, 2, 4)
  112.       AND [tpo].[for_user_vv] IN (1, 2);
  113.       */
  114. LABEL_FINISH:
  115.     --------------------------------------------------------------------
  116.     --/*--<Debug Finish>
  117.     IF @DebugContext IS NOT NULL AND (@DebugLog > 0 OR @DebugPrint > 0 OR @DebugSelect & 8 > 0) BEGIN
  118.         SET @DebugParams = NULL;
  119.  
  120.       EXEC [Debug].[Execution@Finish] @Context = @DebugContext, @RowCount = NULL, @Return = NULL, @Params = @DebugParams, @Log = @DebugLog, @Print = @DebugPrint, @Select = @DebugSelect;
  121.     END
  122.     --*/--</Debug Finish>
  123.     --------------------------------------------------------------------
  124.  
  125.   END TRY
  126.   BEGIN CATCH
  127.     --------------------------------------------------------------------
  128.     --/*--<Debug Finish>
  129.     IF @DebugContext IS NOT NULL AND (@DebugLog > 0 OR @DebugPrint > 0 OR @DebugSelect & 8 > 0) BEGIN
  130.       SET @DebugError     = ERROR_MESSAGE();
  131.       SET @DebugErrorProc = ERROR_PROCEDURE();
  132.       SET @DebugErrorLine = ERROR_LINE();
  133.       SET @DebugParams    = NULL;
  134.       -- Передайте параметр @Return, если это необходимо
  135.       EXEC [Debug].[Execution@Finish] @Context = @DebugContext, @Return = NULL, @Params = @DebugParams, @Error = @DebugError, @ErrorLine = @DebugErrorLine, @ErrorProc = @DebugErrorProc, @Log = @DebugLog, @Print = @DebugPrint, @Select = @DebugSelect;
  136.     END;
  137.     --*/--</Debug Finish>
  138.     --------------------------------------------------------------------
  139.  
  140.     THROW;
  141.   END CATCH
  142. END
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement