CREATE PROCEDURE [dbo].[sp_Auto_Calc_Rows]
    @Sazman_Code VARCHAR(300) ,
    @Start_Date DATETIME ,
    @End_Date DATETIME ,
    @stYears VARCHAR(200) ,
    @Conditions CHAR(1) ,
    @Grade CHAR(1) ,
    @Nezam_No NVARCHAR(10) ,
    @PT_Code VARCHAR(3) ,
    @OrderFlag CHAR(1)
AS
    DECLARE @stYear AS VARCHAR(15)
    DECLARE @stCommAnd AS VARCHAR(8000)
    DECLARE @stCondition AS VARCHAR(250) 
    DECLARE @stOrder AS VARCHAR(100)

    IF @OrderFlag = 1
        SET @stOrder = ' Tarikh_Noskheh , Dumy_Code '
    ELSE
        IF @OrderFlag = 2
            SET @stOrder = ' DrugH.Pt_Code , DrugH.Nezam_No , DrugH.Tarikh_Noskheh ,Dumy_Code '
        ELSE
            IF @OrderFlag = 5
                SET @stOrder = ' DrugH.Tarikh_Noskheh , DrugH.Nezam_No '
            ELSE
                IF @OrderFlag = 6
                    SET @stOrder = ' DrugH.Pt_Code , DrugH.Nezam_No , Dumy_Code , DrugH.Tarikh_Noskheh  '
                ELSE
                    SET @stOrder = ' Meli_Code ,DrugH.Nezam_No , DrugH.Tarikh_Noskheh ,Dumy_Code  '

    SET @stCommAnd = 'Declare @Id_Havaleh Numeric(9) '
        + 'Declare @Dumy_Code varchar(12) '
        + 'Declare @Tarikh_Noskheh SmallDateTime '
        + 'Declare @iRow_Index Int ' + 'Declare @stUpdate_Year varchar(20) '
        + 'Set @iRow_Index = 1  ' + 'Declare cur_List_bime Cursor ' + 'For '
    WHILE CHARINDEX(',', @stYears) <> 0
        BEGIN
   --print @stYears  
            SET @stYear = SUBSTRING(@stYears, 0, CHARINDEX(',', @stYears))
            IF @Conditions = 0
                SET @stCondition = ''
            ELSE
                IF @Conditions = 1
                    SET @stCondition = '  And  ' + @stYear
                        + '.Grade.Status = ' + @Grade
                ELSE
                    IF @Conditions = 2
                        SET @stCondition = '  And  ' + @stYear
                            + '.Grade.Status != ' + @Grade
                    ELSE
                        IF @Conditions = 3
                            SET @stCondition = '  And  ' + @stYear
                                + '.DrugH.Nezam_No = ''' + @Nezam_No + ''''
	
            IF @PT_Code <> ''
                SET @stCondition = @stCondition + '  And  ' + @stYear
                    + '.DrugH.Pt_Code = ' + @PT_Code
 
            SET @stCommAnd = @stCommAnd + 'Select ' + @stYear
                + '.DrugH.Id_Havaleh ,''' + @stYear + ''',' + @stYear
                + '.DrugH.Dumy_Code,' + @stYear + '.DrugH.Tarikh_Noskheh '
                + ' From ' + @stYear + '.DrugH ' + 'Inner Join ' + @stYear
                + '.Doctor   On ' + @stYear + '.Doctor.Nezam_No      = '
                + @stYear + '.DrugH.Nezam_No ' + 'Inner Join ' + @stYear
                + '.Sahmiyeh On ' + @stYear + '.Sahmiyeh.Sazman_Code = '
                + @stYear + '.DrugH.Sazman_Code ' + 'Inner Join ' + @stYear
                + '.Grade    On ' + @stYear + '.DrugH.Grade_Code     = '
                + @stYear + '.Grade.Grade_Code ' + 'Where ' + @stYear
                + '.DrugH.TotPriceSazman > 0 And ' + @stYear
                + '.DrugH.Tarikh_Noskheh Between '''
                + CONVERT(VARCHAR(10), @Start_Date, 111) + ''' And '''
                + CONVERT(VARCHAR(10), @End_Date, 111) + ''' And ' + @stYear
                + '.DrugH.Kind_Flag = 1 And ' + @stYear
                + '.DrugH.Del_Flag = 1 And ' + @stYear
                + '.DrugH.Sazman_Code in(' + @Sazman_Code + ')' + @stCondition  

            SET @stYears = SUBSTRING(@stYears, LEN(@stYear) + 2,
                                     LEN(@stYears) - LEN(@stYear) - 1)
            IF ( LEN(@stYears) > 0 )
                SET @stCommAnd = @stCommAnd + ' UNION '
        END 
    SET @stCommAnd = @stCommAnd + ' Order By ' + @stOrder
    SET @stCommAnd = @stCommAnd + 'Open cur_List_Bime  '
        + 'Fetch Next From cur_List_Bime '
        + 'Into @id_Havaleh , @stUpdate_Year , @Dumy_Code , @Tarikh_Noskheh '
        + 'While @@FETCH_STATUS = 0  ' + 'Begin '
        + 'Exec dbo.sp_UpdateRow  @stUpdate_Year, @Id_Havaleh , @iRow_Index '
        + 'Set @iRow_Index = @iRow_Index + 1 '
        + 'Fetch Next From cur_List_Bime '
        + 'Into @id_Havaleh , @stUpdate_Year , @Dumy_Code , @Tarikh_Noskheh '
        + 'End ' + 'Close cur_List_Bime ' + 'Deallocate  cur_List_Bime '
        + 'print Cast(@iRow_Index-1  as varchar(4)) ' 

    EXECUTE (@stCommAnd)
