CREATE PROCEDURE  [dbo].[Correct_Cheqe]
AS
-----------------------(Find Repeat OrderCheqe Field)--------------------
Declare @ChNo nvarchar(15)
Declare @ChBank Smallint 
Declare @ChPrice Money
Declare @IdCheqe Bigint
Declare @i Smallint
Declare @iCount Smallint
Declare cur_Cheqe Cursor Local For
Select Ch_No, Ch_Bank, Ch_Price
From Cheqe
Where Ch_No Is Not Null
Group By Ch_No,OrderCheqe, Ch_Bank, Ch_Price
Having Count(OrderCheqe)> 1
Open cur_Cheqe
Fetch Next From cur_Cheqe Into @ChNo, @ChBank, @ChPrice
While @@Fetch_Status = 0 
Begin
  Declare cur_Into Cursor Local For
  Select Id_Cheqe From Cheqe
  Where Ch_No = @ChNo And Ch_Bank = @ChBank
  And Ch_Price = @ChPrice
  Order By Id_Cheqe
  Open cur_Into 
  Fetch Next FRom cur_Into Into @IdCheqe
  Set @i = 0
  Select @ICount = Count(Id_Cheqe) From Cheqe 
  Where Ch_No = @ChNo And Ch_Bank = @ChBank
  And Ch_Price = @ChPrice
  While @i < @iCount
  begin
    Update Cheqe Set OrderCheqe = @i Where Id_Cheqe  = @IdCheqe And Ch_No = @Chno  
                					        And Ch_Bank = @ChBank
							        And Ch_Price = @ChPrice
    Set @i = @i + 1
    Fetch Next FRom cur_Into Into @IdCheqe
  end
  Close cur_Into
  Deallocate cur_Into
end
Close cur_Cheqe
Deallocate cur_Cheqe

--------------------------------Correct One Record From Two Bank ---------------------

Update Cheqe Set Ch_pass_New = Ch_Pass From Cheqe Where Guid_Cheqe in 
(
Select Cheqe.Guid_Cheqe 
From Cheqe 
Inner Join (
		Select Ch_No, Count(Ch_No) CountCh_No , Ch_Bank 
    From (
		        Select Ch_No, Ch_Bank, Ch_Pass_New From Cheqe 
        		Where Ch_No Is Not Null 
         		Group By Ch_No, Ch_Bank, Ch_Pass_New
         ) as Table1
		Group By Ch_No , Ch_Bank 
		Having Count(Ch_No) = 1
 ) as Table2 On 
(Table2.Ch_No = Cheqe.Ch_No) AND (Table2.Ch_Bank = Cheqe.Ch_Bank) 

Where Cheqe.Ch_Pass_New = '20' And Cheqe.Ch_Pass_New <> Cheqe.Ch_Pass)

--------------------------------Correct One Record Cheqe -----------------------------
Update Cheqe Set Ch_pass_New = Ch_Pass From Cheqe Where Guid_Cheqe in (
Select Cheqe.Guid_Cheqe From Cheqe 
Inner Join (
		Select Ch_No, Count(Ch_No) CountCh_No From (
		Select Ch_No, Ch_Bank, Ch_Pass_New From Cheqe 
		Where Ch_No Is Not Null 
		Group By Ch_No, Ch_Bank, Ch_Pass_New) as Table1
		Group By Ch_No
		Having Count(Ch_No) = 1) as Table2 On (Table2.Ch_No = Cheqe.Ch_No)
Where Cheqe.Ch_Pass_New = '20' And Cheqe.Ch_Pass_New <> Cheqe.Ch_Pass)

--------------------------------Correct Two Record Cheqe -----------------------------
Update Cheqe Set Ch_Pass_New = '20' Where Guid_Cheqe in (
Select Cheqe.Guid_Cheqe From Cheqe 
Inner Join 
(
Select Ch_No, Ch_Bank From Cheqe 
Where Ch_No Is Not Null 
Group By Ch_No, Ch_Bank
Having Count(Ch_No) > 1) as Table1 On (Table1.Ch_No = Cheqe.Ch_No) AND (Table1.Ch_Bank = Cheqe.Ch_Bank)
Where Cheqe.PreGuid_Cheqe Is Null And Cheqe.Ch_Pass_New <> '20')

-----------------------(Find Repeat OrderCheqe Field)--------------------
Declare @Ch_No nvarchar(15)
Declare @Ch_Bank Smallint 
Declare @Ch_Price Money
Declare @Id_Cheqe Bigint
Declare @i_Count Smallint
Declare cur_Cheqe Cursor Local For
Select Ch_No, Ch_Bank, Ch_Price
From Cheqe
Where Ch_No Is Not Null
Group By Ch_No,OrderCheqe, Ch_Bank, Ch_Price
Having Count(OrderCheqe)> 1
Open cur_Cheqe
Fetch Next From cur_Cheqe Into @Ch_No, @Ch_Bank, @Ch_Price
While @@Fetch_Status = 0 
Begin
  Declare cur_Into Cursor Local For
  Select Id_Cheqe From Cheqe
  Where Ch_No = @Ch_No And Ch_Bank = @Ch_Bank
  And Ch_Price = @Ch_Price
  Order By Id_Cheqe
  Open cur_Into 
  Fetch Next FRom cur_Into Into @Id_Cheqe
  Set @i = 0
  Select @I_Count = Count(Id_Cheqe) From Cheqe 
  Where Ch_No = @Ch_No And Ch_Bank = @Ch_Bank
  And Ch_Price = @Ch_Price
  While @i < @i_Count
  begin
    Update Cheqe Set OrderCheqe = @i Where Id_Cheqe  = @Id_Cheqe And Ch_No = @Ch_no  
                					        And Ch_Bank = @Ch_Bank
							        And Ch_Price = @Ch_Price
    Set @i = @i + 1
    Fetch Next FRom cur_Into Into @Id_Cheqe
  end
  Close cur_Into
  Deallocate cur_Into
end
Close cur_Cheqe
Deallocate cur_Cheqe

-------------------------(Update Field PreGuid_Cheqe)--------------------------------
Declare @PreGuid_Cheqe uniqueidentifier
Declare @GuidCheqe uniqueidentifier

Declare cur_Cheqe Cursor Local For
Select  Ch_No, Ch_Bank, Ch_Price  From Cheqe
Where Ch_No Is Not Null 
Group By Ch_No, Ch_Price, Ch_Bank

Open cur_Cheqe
Fetch Next From cur_Cheqe Into @ChNo, @ChBank, @ChPrice
While @@Fetch_Status = 0 
Begin
  Declare cur_Into Cursor Local For
  Select Guid_Cheqe From Cheqe
  Where Ch_No = @ChNo And Ch_Bank = @ChBank
  And Ch_Price = @ChPrice
  Order By OrderCheqe
  Open cur_Into 
  Fetch Next FRom cur_Into Into @GuidCheqe
  Set @i = 0
  Select @ICount = Count(Id_Cheqe) From Cheqe 
  Where Ch_No = @ChNo And Ch_Bank = @ChBank
  And Ch_Price = @ChPrice
  While @i < @iCount
  begin
    IF @i > 0  
      Update Cheqe Set PreGuid_Cheqe = @PreGuid_Cheqe Where Guid_Cheqe  = @GuidCheqe 
    Set @i = @i + 1
    Set @PreGuid_Cheqe = @GuidCheqe
    Fetch Next FRom cur_Into Into @GuidCheqe
  end
  Close cur_Into
  Deallocate cur_Into
  Fetch Next From cur_Cheqe Into @ChNo, @ChBank, @ChPrice
end
Close cur_Cheqe
Deallocate cur_Cheqe

--------------------Correct Same Two Cheqe ----------------------------
Declare cur_Cheqe Cursor Local For
Select Ch_No  From Cheqe
Where Ch_No Is Not Null
Group By Ch_No , Ch_Price, Ch_Tarikh
Having Count(Ch_No) > 1 

Open cur_Cheqe
Fetch Next From cur_Cheqe Into @ChNo
While @@Fetch_Status = 0 
Begin
  Declare cur_Ins Cursor Local For
  Select Guid_Cheqe  From Cheqe 
  Where Ch_No = @ChNo
  Order By OrderCheqe
  Open cur_Ins
  Fetch Next From cur_Ins Into @GuidCheqe

  Set @i = 0
  Select @ICount = Count(Ch_No) From Cheqe Where Ch_No = @ChNo

  While @@Fetch_Status = 0 
  Begin
    if @i < @iCount - 1 
      Update Cheqe Set Ch_Pass_New = '20' Where Guid_Cheqe = @GuidCheqe And Ch_Pass_New <> '20'
    Else
      Update Cheqe Set Ch_Pass_New = Ch_Pass Where Guid_Cheqe = @GuidCheqe And Ch_Pass_New = '20'
      
    Set @i = @i + 1
    Fetch Next From cur_Ins Into @GuidCheqe
  End
  Close cur_Ins
  Deallocate cur_Ins

  Fetch Next From cur_Cheqe Into @ChNo
end
Close cur_Cheqe
Deallocate cur_Cheqe






