I am making a python project, in LangGraph and using LLM models, that takes an colleague question about doing SQL queries, generates a query with a query generator, then a query validator evaluates it and if there is a problem a query fixer fixes it, but the problem is that my query validator keep repeating the same rules broken each time. My query fixer keeps getting the same errors all the time and tries to keep fixing things that shouldn't. Every node uses Llama 3.1 70B for inferences.

Can you help me decide what to do, which one is wrong or if both are correct and my approach is the one with error. I don't know what to do to solve this, propose me ideas an alternatives.

+++++++++++++++++
User Query: 
"delete on cob_salaries..cn_down_adh for COMPANY = 10 and BENEFIT = 2."
+++++++++++++++++
Ground Truth: 
"--INPUT; delete on cob_reca..re_invoices_da for agreement = 10 and sequential invoice = 2 and status N

  use cob_reca
  go

  declare
  @w_k_records int,
  @w_n_error int,
  @w_d_error varchar(54)

  select @w_n_n_error = 0

  print 'RECORDS TO REMOVE' print 'RECORDS TO REMOVE' print 'RECORDS TO REMOVE

  delete cob_reca..re_invoices_da
  from cob_reca..re_invoices_da
  where fa_cod_conv = 10
  and fa_sec_invoices = 2
  and fa_status = 'N

  select @w_k_records = @@rowcount , @w_n_error = @@error

  if @w_n_error <> 0
  begin
    select @w_n_n_error = 107540
    select @w_d_error = 'ERROR WHEN DELETING TABLE RE_INVOICING_DA_TABLE
    goto END
  end


  if @w_k_records = 0
  begin
    select @w_n_error = 900131
    select @w_d_error = 'ERROR DOES NOT EXIST RE_INVOICES_DA_TABLE RE_INVOICES_DA'
    goto END
  end

  END:
  select @w_n_error
  select @w_d_error

  select 'QUANTITY OF REMOVED RECORDS
  select @w_k_records
  go"
+++++++++++++++++
Query Generator:
"```sql
use cob_sueldos
go

declare
@w_k_registros int,
@w_n_error     int,
@w_d_error     varchar(54)

select @w_n_error = 0

print 'REGISTROS A ELIMINAR'

delete cob_sueldos..cn_baja_adh
from   cob_sueldos..cn_baja_adh
where  ba_id_emp     = 10
and    ba_prestacion = 2

select @w_k_registros = @@rowcount , @w_n_error = @@error

if @w_n_error <> 0
begin
  select @w_n_error = 107540
  select @w_d_error = 'ERROR AL BORRAR TABLA CN_BAJA_ADH'
  goto FIN
end

if @w_k_registros = 0
begin
  select @w_n_error = 900131
  select @w_d_error = 'ERROR NO EXISTE REGISTRO TABLA CN_BAJA_ADH'
  goto FIN
end

FIN:
select @w_n_error
select @w_d_error

select 'CANTIDAD REGISTROS ELIMINADOS'
select @w_k_registros
go
```"
+++++++++++++++++
Query Validator (1st time):
"'VALIDATION_ERROR: 

The generated SQL query does not meet several quality standards set in the provided rules documents.

1. **Lack of comments**: The query lacks comments describing the code section and its purpose. According to the rules, comments should be used to explain the functionality of the code.

2. **Error is not properly handled**: Although the query includes an error check after the DELETE statement, the error is not handled according to the established rules. There should be a specific block of code for error handling and not simply assigning a value to a variable.

3. **GoTo tags are not used properly**: The query uses the “GoTo” tag to jump to a section of code in case of an error, but the tag is not clearly defined and a blank line is not left before it, as stated in the rules.

4. **Lack of documentation**: The query does not include documentation on parameters, returned results, or dependencies, which is required by the rules.

5. **Failure to follow the format established for error messages**: The error messages do not follow the format established in the rules, which can generate confusion.

6. **The number of affected records is not controlled**: The query does not check if the number of deleted records is the expected one, which can generate errors.

7. **Failure to use the “USE” statement properly**: The “USE” statement is used to select the database, but it does not check if the database exists or if it can be used.

8. **Declaration variables are not used properly**: Variables are declared implicitly, but their type and length are not specified, which can lead to errors.

9. **Variable names do not follow the format established for variable names**: Variable names do not follow the format established in the rules, which may cause confusion.

10. **Table existence is not checked before deleting records**: The query does not check if the table exists before deleting records, which may cause errors."
+++++++++++++++++
Query Fixer (1st time):
"```sql
-- Select the database to perform the operation on
USE cob_salaries
GO

-- Declaration of variables with their respective types and lengths
DECLARE 
  @w_k_records INT, 
  @w_n_error INT, 
  @w_d_error VARCHAR(54), 
  @w_b_exists_table BIT

-- Initialize error variable
SET @w_n_n_error = 0

-- Check if the table exists before deleting records
IF OBJECT_ID('cob_sueldos..cn_baja_adh', 'U') IS NOT NULL
BEGIN
  SET @w_b_exists_table = 1
END
ELSE
BEGIN
  SET @w_b_exists_table = 0
END

-- If the table does not exist, assign an error
IF @w_b_exists_table = 0
BEGIN
  SET @w_n_error = 900130
  SET @w_d_error = 'ERROR DOES NOT EXIST TABLE CN_LOWER_ADH'.
  GOTO END
END

-- Display the message of records to delete
PRINT 'RECORDS TO DELETE

-- Deletes the records from the table
DELETE cob_sueldos..cn_baja_adh
FROM cob_sueldos..cn_baja_adh
WHERE ba_id_emp = 10
AND ba_benefit = 2

-- Verify the number of records affected
SET @w_k_records = @@ROWCOUNT

-- Check if an error occurred
SET @w_n_error = @@ERROR

-- If an error occurred, handles the error
IF @w_n_error <> 0
BEGIN
  -- Assigns a custom error
  SET @w_n_n_error = 107540
  SET @w_d_error = 'ERROR ON DELETE TABLE CN_LOWER_ADH'.
  GOTO END
END

-- If no records were deleted, assigns an error
IF @w_k_records = 0
BEGIN
  SET @w_n_error = 900131
  SET @w_d_error = 'ERROR DOES NOT EXIST RECORD TABLE CN_DOWN_ADH'
  GOTO END
END

-- Error handling label
END:
  -- Displays error message or number of deleted records.
  IF @w_n_error <> 0
  BEGIN
    SELECT 'ERROR: ' + CONVERT(VARCHAR, @w_n_error)
    SELECT @w_d_error
  END
  ELSE
  BEGIN
    SELECT 'QUANTITY OF REMOVED RECORDS: ' + CONVERT(VARCHAR, @w_k_records)
  END
GO
```"
+++++++++++++++++
Query Validator (2nd time):
"'VALIDATION_ERROR: The generated code does not comply with several quality standards established in the rules documents.

1. No section comments have been used to explain the logic of the code. For example, a comment could have been added to explain the condition `IF @w_b_exists_table = 0` and why an error is assigned in that case.

2. The `GoTo` tag has not been used effectively. Instead of using “GOTO FIN”, a TRY-CATCH block could have been used to handle errors more efficiently.

3. The stored procedure has not been documented with the required information, such as column detail, query result and success value.

4. Keywords have not been used to search CHM for the stored procedure.

5. The dependencies of the stored procedure (dynamic SQL) have not been documented.

6. XML special characters have not been used to document the code.

7. The code does not follow a consistent indentation pattern, which can make it difficult to read and understand.

8. Variables with descriptive names have not been used, which may make it difficult to understand the purpose of each variable.

9. A BEGIN-END block has not been used to group the instructions to be executed in case of an error.

10. A TRY-CATCH block has not been used to handle errors more efficiently.

In short, the generated code does not comply with several quality standards established in the rules documents, which may affect its readability, maintainability and efficiency.'"
+++++++++++++++++
Query Fixer (2nd time):
"```sql
-- Select the database to perform the operation on
USE cob_salaries
GO

-- Declaration of variables with their respective types and lengths
DECLARE 
  @w_k_records INT, 
  @w_n_error INT, 
  @w_d_error VARCHAR(254), 
  @w_b_exists_table BIT

-- Initialize error variable
SET @w_n_n_error = 0

-- Checks if the table exists before deleting records
IF OBJECT_ID('cob_sueldos..cn_baja_adh', 'U') IS NOT NULL
BEGIN
  SET @w_b_exists_table = 1
END
ELSE
BEGIN
  SET @w_b_exists_table = 0
END

-- If the table does not exist, assign an error
IF @w_b_exists_table = 0
BEGIN
  SET @w_n_error = 900130
  SET @w_d_error = 'ERROR DOES NOT EXIST TABLE CN_LOWER_ADH'.
  GOTO END
END

-- Display the message of records to delete
PRINT 'RECORDS TO DELETE

BEGIN TRY
  -- Deletes records from the table
  DELETE 
    cn_delete_adh
  FROM 
    cob_sueldos..cn_baja_adh
  WHERE 
    ba_id_emp = 10
    AND ba_benefit = 2

  -- Verify the number of records affected
  SET @w_k_records = @@ROWCOUNT

  -- Check if an error occurred
  SET @w_n_error = @@ERROR
END TRY
BEGIN CATCH
  -- Assign a custom error
  SET @w_n_n_error = 107540
  SET @w_d_error = 'ERROR WHEN DELETING TABLE CN_LOW_ADH' SET @w_d_error = 'ERROR WHEN DELETING TABLE CN_LOW_ADH
END CATCH

-- If an error occurred, handle the error
IF @w_n_error <> 0
BEGIN
  GOTO END
END

-- If no records were deleted, assigns an error
IF @w_k_records = 0
BEGIN
  SET @w_n_error = 900131
  SET @w_d_error = 'ERROR DOES NOT EXIST RECORD TABLE CN_DOWN_ADH'
  GOTO END
END

-- Error handling label
END:
  -- Displays error message or number of deleted records.
  IF @w_n_error <> 0
  BEGIN
    SELECT 'ERROR: ' + CONVERT(VARCHAR, @w_n_error)
    SELECT @w_d_error
  END
  ELSE
  BEGIN
    SELECT 'QUANTITY OF REMOVED RECORDS: ' + CONVERT(VARCHAR, @w_k_records)
  END
GO
```"
+++++++++
From here on, they keep looping between each other.