0

我有一个存储过程,它的任务是,它将获取一个文件并将记录传输到 TEMP 表中,然后相应地进行验证,然后将记录放入 Main Staging 表中。(此外,我们还有 3 个表进行验证目的,它们是 StockIntegrityCheck、IntegrityErrorLog、InterfaceIntegrity)

问题:当一个空文件出现(0KB 文件)时,它不应该在任何表中放置任何条目。但它在不需要的 InteggrityErrorLog 表中添加了一个条目。我想相应地更正存储过程?

这是SP:

USE [PARAM.DB]

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].        [usp_LDMDB_GMIH_DSOA_STAGING]') AND type in (N'P'))
DROP PROCEDURE [dbo].[usp_LDMDB_GMIH_DSOA_STAGING]

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*  
Description:    This program will insert records from LDMDB_GMIH_DSO_A_STAGING_TEMP   table to LDMDB_GMIH_DSO_A_STAGING table
*/  

CREATE PROCEDURE [dbo].[usp_LDMDB_GMIH_DSOA_STAGING] 
AS

DECLARE
  @DATA VARCHAR(200),
  @HDRDATA VARCHAR(200),
  @FLAG INT,
  @ErrStmt VARCHAR(500),
  @ProcessDate datetime,
  /* Stock Integrity Changes for I0180 Starts */
  @Integritycheck INT,
  @Totalcount VARCHAR(10),
  @Totalquantity VARCHAR(10),
  @TrackingID VARCHAR(20),
  @count INT,
  @quantity INT;    
  /* Stock Integrity Changes for I0180 Ends */
BEGIN
  BEGIN TRANSACTION
  select @ProcessDate =  GETDATE()
  /* Stock Integrity Changes for I0180 Starts */
  SET @count = 0;
  SET @quantity = 0;
  SET @Integritycheck = (SELECT Value from dbo.LDMStockIntegrityCheck where interfacename  = 'I0180' and filename = 'PIZ.HW.NDC.CALLOFF.FILE.LDM');

  /* Stock Integrity Changes for I0180 Ends */
  DECLARE CUR_JOB CURSOR LOCAL 
  FOR
  SELECT DATA FROM LDMDB_GMIH_DSO_A_STAGING_TEMP ORDER BY ROW_ID 
  OPEN CUR_JOB

  IF @@ERROR <> 0  
  BEGIN
    SET @ErrStmt= 'Error while Opening the Cursor'  
    GOTO ERRHDL  
  END  
  -- LOOPING THROUGH EACH RECORD.
  FETCH NEXT FROM CUR_JOB INTO @DATA

  IF @@ERROR <> 0  
  BEGIN  
    SET @ErrStmt= 'Error while Fetching the Record From Cursor'  
    GOTO ERRHDL  
  END

  WHILE @@FETCH_STATUS = 0
  BEGIN
    SET @FLAG = 0
    IF(SUBSTRING (@DATA,1,1)= '1')
    BEGIN
      SET @HDRDATA = @DATA
      SET @FLAG = 0
      /* Stock Integrity Changes for I0180 Starts */

      IF (@Integritycheck = 1 )
      BEGIN
        SET @TrackingID   = SUBSTRING(@data,19,20);
        SET @Totalcount   =  CONVERT(INT,SUBSTRING(@data,39,10));
        SET @Totalquantity = CONVERT(INT,SUBSTRING(@data,49,15));
      END

      /* Stock Integrity Changes for I0180 Ends */       
    END

    IF(SUBSTRING (@DATA,1,1)= '5')
    BEGIN
      SET @FLAG = 1
      /* Stock Integrity Changes for I0180 Starts */
      IF (@Integritycheck = 1 )
      BEGIN
        SET @count= @count +1 ;
        SET @quantity = @quantity + CONVERT(INT,SUBSTRING(@data,45,09)) ;
      END
      /* Stock Integrity Changes for I0180 Ends */
    END

    IF (@FLAG = 1)
    BEGIN
      INSERT INTO LDMDB_GMIH_DSO_A_STAGING 
      (    FileCreationDate,
           GenerationNumber,
           WAREHOUSE_CODE,
           ALLOC_RUN_DATE,
           SUPP_CODE,
           SUPP_LOC_CODE,
           PICKSHEET_NUM,
           UPC,
           DELIVERY_POINT,
           STORE_NUM,
           VALUE_TOTAL_SEND,
           COUNTRY_CODE,
           DISPATCH_PRIORITY,
           QTY,
           ProcessDate
      )
      SELECT  LTRIM(RTRIM(SUBSTRING(convert( varchar(20),(CONVERT(datetime, SUBSTRING  (@HDRDATA,2,8), 111)),121),1,10))) FileCreationDate,
              LTRIM(RTRIM(SUBSTRING (@HDRDATA,10,9))) GenerationNumber,
              LTRIM(RTRIM(SUBSTRING (@DATA,2,3)))     WAREHOUSE_CODE ,
              LTRIM(RTRIM(SUBSTRING(convert( varchar(20),(CONVERT(datetime, SUBSTRING (@DATA,5,8), 111)),121),1,10))) ALLOC_RUN_DATE,
              RIGHT(REPLICATE('0',10) + LTRIM(RTRIM(SUBSTRING (@DATA,13,4))),5)  SUPP_CODE,
              LTRIM(RTRIM(SUBSTRING (@DATA,17,5)))  SUPP_LOC_CODE,
              LTRIM(RTRIM(SUBSTRING (@DATA,22,6)))    PICKSHEET_NUM,
              LTRIM(RTRIM(SUBSTRING (@DATA,29,8)))    UPC,
              LTRIM(RTRIM(SUBSTRING (@DATA,37,4)))    DELIVERY_POINT,
              LTRIM(RTRIM(SUBSTRING (@DATA,41,4)))    STORE_NUM, 
              (SELECT CASE  WHEN cast(SUBSTRING (@DATA,45,9) AS NUMERIC) < 0 THEN  ('-' +  LTRIM(RTRIM(SUBSTRING (@DATA,46,8)))) ELSE LTRIM(RTRIM(SUBSTRING (@DATA,45,9))) END) VALUE_TOTAL_SEND,
              LTRIM(RTRIM(SUBSTRING (@DATA,54,3)))    COUNTRY_CODE, 
              LTRIM(RTRIM(SUBSTRING (@DATA,57,2)))    DISPATCH_PRIORITY,
              LTRIM(RTRIM(SUBSTRING (@DATA,59,9)))    QTY,
              @ProcessDate

      IF @@ERROR <> 0  
      BEGIN  
        SET @ErrStmt= 'Error while Inserting the Records into the Staging Table'  
        GOTO ERRHDL  
      END
    END

    FETCH NEXT FROM CUR_JOB INTO @DATA
  END

  CLOSE CUR_JOB
  DEALLOCATE CUR_JOB
  IF (@Integritycheck = 0)
  BEGIN
    COMMIT TRANSACTION
  END

  FINISH:
  BEGIN
    /* Stock Integrity Changes for I0180 Starts */
    IF (@Integritycheck = 0 )
    BEGIN 
      SET @ErrStmt= 'Inserting the Records into the Staging Table Successful'
      PRINT 'Inserting the Records into the Staging Table Successful'
      Return
    END

    ELSE
    BEGIN

      IF ((@count = @Totalcount)AND(@quantity = @Totalquantity))
      BEGIN
        INSERT INTO dbo.LDMInterfaceIntegrity VALUES ('I0180','PIZ.HW.NDC.CALLOFF.FILE.LDM',@TrackingID,@Totalquantity,@Totalcount,@ProcessDate);
        COMMIT TRANSACTION;             
        BEGIN  
          SET @ErrStmt= 'Inserting the Records into the Staging Table Successful';
          Return
        END      

      END
      ELSE
      BEGIN 
        ROLLBACK TRANSACTION;
        IF (@count <> @Totalcount)
        BEGIN                                   
          INSERT INTO dbo.LDMIntegrityErrorLog VALUES ('I0180','PIZ.HW.NDC.CALLOFF.FILE.LDM',@TrackingID,'Count Mismatch',@ProcessDate);
          PRINT 'Count mismatch';
        END
        ELSE
        BEGIN

          INSERT INTO dbo.LDMIntegrityErrorLog VALUES ('I0180','PIZ.HW.NDC.CALLOFF.FILE.LDM',@TrackingID,'Quantity Mismatch',@ProcessDate);
          PRINT 'Quantity Mismatch';
        END

        RETURN;
      END
    END
    /* Stock Integrity Changes for I0180 Ends */
  END
  ERRHDL:  
  BEGIN 
    ROLLBACK TRANSACTION 
    RETURN              
  END

END
4

2 回答 2

0

按此使用RAISERROR ... WITH NOWAIT(以及许多其他):http ://www.mssqltips.com/sqlservertip/1660/using-the-nowait-option-with-the-sql-server-raiserror-statement/

在某些错误级别,RAISERROR实际上并不认为是错误,因此它可以用作PRINT.

于 2013-07-07T16:57:24.280 回答
0

Sql server management studio 提供调试存储过程的工具。通过调试存储过程,您可以检查每个变量的值以及存储过程执行期间执行的语句。只需将调试点放在存储过程中的语句中,然后按 debug 。

检查http://www.youtube.com/watch?v=618LE_FZCxI

于 2013-07-07T16:59:14.400 回答