与Firebird SUSPEND

时间:2017-03-13 18:16:19

标签: sql-server sql-server-2012 firebird firebird2.5

我有一个来自Firebird数据库的存储过程,该数据库通过该过程多次调用SUSPEND。我不是这个程序的作者,也不是我非常擅长Firebird语法,但是Firebird docs说明了以下内容:

  

使用SUSPEND将一行数据从一个过程返回给其调用者。

我正在使用将过程转换为SQL Server等效项的软件,但是唯一未转换的部分是SUSPEND语句。所以,我已经评论过它来获取在SQL Server中创建的过程,但现在当我执行它时,我没有得到任何数据。在转换的SQL Server版本中,SUSPEND发生在游标循环中。在Firebird版本中,它出现在for循环中。

我正在执行这样的程序......

EXEC MY_CONVERTED_FB_PROCEDURE ARG1,ARG2,ARG3, etc...

在Firebird中,我通常会这样做

SELECT * FROM MY_FB_PROCEDURE (ARG1,ARG2,ARG3,...)

给了我结果集。

我的问题是,是否有人可以分享他们可以共享的自定义函数或代码片段,我可以添加代替SUSPEND语句,以便SQL Server将像Firebird一样执行相同的过程?

完全转换的SQL Server程序(已删除SUSPEND)

CREATE PROCEDURE ACCIDENT_SUMMARY_V2 
@IN_LOGINID INT,
    @IN_CUSTID VARCHAR(6),
    @IN_CUSTDIV VARCHAR(6),
    @IN_UNIT VARCHAR(6),
    @STARTDATE DATE,
    @ENDDATE DATE,
    @MONTHLY_BREAKDOWN_FLAG CHAR(1),
    @IN_GROUPBY VARCHAR(20)

AS
begin

   declare @ONE_ACCT_REF INT
   declare @TOT_ACCIDENT_COUNT INT
   declare @NUM_MONTHS FLOAT
   declare @YR_START VARCHAR(4)
   declare @YR_END VARCHAR(4)
   declare @SAV_MMM_YYYY VARCHAR(15)
   declare @SAV_GROUPBY VARCHAR(20)
   declare @SAV_ACCIDENT_COUNT INT
   DECLARE @YYYY_MM INT
   DECLARE @MMM_YYYY VARCHAR(15)
   DECLARE @GROUPBY VARCHAR(80)
   DECLARE @SUBTOTAL_FLAG CHAR(1)
   DECLARE @EXPENSE_MONTH DATE
   DECLARE @MILES_DRIVEN INT
   DECLARE @CUSTDIV VARCHAR(6)
   DECLARE @UNIT VARCHAR(6)
   DECLARE @ACCT_REF INT
   DECLARE @CURRENT_MMM_YYYY VARCHAR(15)
   DECLARE @CURRENT_YYYY_MM VARCHAR(15)
   DECLARE @BILLTOTAL NUMERIC(15,2)
   DECLARE @TOT_EXP_GLASS NUMERIC(15,2)
   DECLARE @TOT_EXP_SUBROGATE NUMERIC(15,2)
   DECLARE @TOT_EXP_BODYWORK NUMERIC(15,2)
   DECLARE @TOT_EXP_RENT NUMERIC(15,2)
   DECLARE @TOT_EXP_SURCHARGE NUMERIC(15,2)
   DECLARE @TOT_EXP_TAXES NUMERIC(15,2)
   DECLARE @TOT_EXP_TOW NUMERIC(15,2)
   DECLARE @ACCIDENT_COUNT INT
   DECLARE @DRIVER_AT_FAULT_COUNT INT
   DECLARE @DRY_ROAD_COUNT INT
   DECLARE @NOTDRY_ROAD_COUNT INT
   DECLARE @CLASS_REAREND_COUNT INT
   DECLARE @CLASS_PARKING_COUNT INT
   DECLARE @CLASS_PARKED_COUNT INT
   DECLARE @SWV_cursor_var1 CURSOR
   SET @DRIVER_AT_FAULT_COUNT = 0
   SET @ACCIDENT_COUNT = 0
   SET @DRY_ROAD_COUNT  = 0
   SET @NOTDRY_ROAD_COUNT = 0
   SET @TOT_ACCIDENT_COUNT = 0

   SET @CURRENT_YYYY_MM = null
   if ((@IN_GROUPBY is null OR  @IN_GROUPBY = ''))
      SET @IN_GROUPBY = 'nogroup'


   if ((@IN_UNIT is not null AND  @IN_UNIT <> ''))
      select @ONE_ACCT_REF = acct_id from custacct ca
      where ca.custID = @IN_CUSTID
      and ca.unit = @IN_UNIT
ELSE
   SET @ONE_ACCT_REF = null

   SET @SWV_cursor_var1 = CURSOR  FOR (** SELECT STATEMENT HERE **)

   OPEN @SWV_cursor_var1

   FETCH NEXT FROM @SWV_cursor_var1 INTO @YYYY_MM,@MMM_YYYY,@GROUPBY,@BILLTOTAL,@TOT_EXP_GLASS,@TOT_EXP_SUBROGATE,
   @TOT_EXP_BODYWORK,@TOT_EXP_RENT,@TOT_EXP_TOW,@TOT_EXP_SURCHARGE,@TOT_EXP_TAXES,
   @DRIVER_AT_FAULT_COUNT,@DRY_ROAD_COUNT,@NOTDRY_ROAD_COUNT, 
   @CLASS_REAREND_COUNT,@CLASS_PARKING_COUNT,@CLASS_PARKED_COUNT,@ACCIDENT_COUNT

   while @@FETCH_STATUS = 0
   begin
      if ((@MONTHLY_BREAKDOWN_FLAG = 'T') and (@YYYY_MM <>  @CURRENT_YYYY_MM) and ((@CURRENT_YYYY_MM is not null AND  @CURRENT_YYYY_MM <> ''))
      and @IN_GROUPBY <> 'nogroup')
      begin
         SET @SAV_MMM_YYYY      = @MMM_YYYY
         SET @SAV_GROUPBY       = @GROUPBY
         SET @SAV_ACCIDENT_COUNT      = @ACCIDENT_COUNT

          --miles_driven  = :tot_miles_driven;
         SET @ACCIDENT_COUNT = @TOT_ACCIDENT_COUNT
         SET @SUBTOTAL_FLAG = 'T'
         SET @GROUPBY = 'Subtotal'
         SET @MMM_YYYY = @CURRENT_MMM_YYYY
         --suspend
         SET @MMM_YYYY      = @SAV_MMM_YYYY
         SET @GROUPBY       = @SAV_GROUPBY
         SET @ACCIDENT_COUNT = @SAV_ACCIDENT_COUNT
      end
      SET @CURRENT_YYYY_MM = @YYYY_MM
      SET @CURRENT_MMM_YYYY = @MMM_YYYY
      SET @SUBTOTAL_FLAG = 'F'

       --tot_miles_driven  = :tot_miles_driven + :miles_driven;
      SET @TOT_ACCIDENT_COUNT = @TOT_ACCIDENT_COUNT+@ACCIDENT_COUNT
      SET @YR_START = year(@STARTDATE)
      SET @YR_END = year(@ENDDATE)
      SET @YR_START = SUBSTRING(@YR_START,3,4)
      SET @YR_END = SUBSTRING(@YR_END,3,4)
      --suspend
      FETCH NEXT FROM @SWV_cursor_var1 INTO @YYYY_MM,@MMM_YYYY,@GROUPBY,@BILLTOTAL,@TOT_EXP_GLASS,@TOT_EXP_SUBROGATE,
      @TOT_EXP_BODYWORK,@TOT_EXP_RENT,@TOT_EXP_TOW,@TOT_EXP_SURCHARGE,@TOT_EXP_TAXES,
      @DRIVER_AT_FAULT_COUNT,@DRY_ROAD_COUNT,@NOTDRY_ROAD_COUNT, 
      @CLASS_REAREND_COUNT,@CLASS_PARKING_COUNT,@CLASS_PARKED_COUNT,@ACCIDENT_COUNT
   end
   CLOSE @SWV_cursor_var1

         -- miles_driven  = :tot_miles_driven;
   if ((@MONTHLY_BREAKDOWN_FLAG = 'T') and ((@CURRENT_YYYY_MM is not null AND  @CURRENT_YYYY_MM <> '')) and (@IN_GROUPBY <> 'nogroup'))
   begin
      SET @SUBTOTAL_FLAG = 'T'
      SET @GROUPBY = 'Subtotal'
      --suspend
   end
   RETURN
END

0 个答案:

没有答案