Stored Procedure for Invoices

This is my first Stored Procedure.

I have a file Test01 with a list of Invoice Numbers. The Invoice File (Invoices02) has many invoices where each invoice could have 5 or more rows.

The Test01 file starts with all rows for Field1 as blank. As each invoice number is read from Test01 then Field1 is marked as "X".

As each invoice number is read from the Test01 file, all rows in the Invoice file are marked as EXTRA1 = "X" for that Invoice Number.

I have developed a Stored Procedure as below to do this but it doesn't work. The error message is not meaningful. I would appreciate any guidance on this.

Mike

DELIMITER $$

CREATE PROCEDURE [IF NOT EXISTS] markInv02

BEGIN
    
  DECLARE count1 INT DEFAULT 1;
  
  WHILE count1 > 0
  
  count1 = SELECT COUNT(invnbr) from Test01 where Field1 = " ";
  
    if count1 > 0;     
      SELECT invnbr INTO @invValue FROM Test01 WHERE Field1 = "  ";
  
      update Test01 set Field1 = "X" where invnbr = @invValue;
  
      UPDATE Invoices02 set EXTRA = "X" where invnbr = @invValue;
    endif;
  END WHILE;

END$$

DELIMITER ;

Hi Mike,

What is the error message then?

Here is the error message.
Mike
pict3

Have you turned prepared statements of or off in your resource?

How are you using this in your app? Just wondering why you create the stored procedure from Retool app and not workbench in your database.

How do I turn "prepared statements" off?

Mike

DELIMITER $$

CREATE PROCEDURE `markInv02`()
BEGIN
  DECLARE count1 INT;
  DECLARE invValue INT;

  SET count1 = (SELECT COUNT(invnbr) FROM Test01 WHERE Field1 = " ");

  WHILE count1 > 0 DO
    SET invValue = (SELECT invnbr FROM Test01 WHERE Field1 = " ");
    
    UPDATE Test01 SET Field1 = "X" WHERE invnbr = invValue;
    UPDATE Invoices02 SET EXTRA = "X" WHERE invnbr = invValue;
    
    SET count1 = (SELECT COUNT(invnbr) FROM Test01 WHERE Field1 = " ");
  END WHILE;
END$$

DELIMITER ;

Mark:

I can get stored procedures to work in Workbench but not in Retool.

I am attaching 2 screen shots. There are no errors reported in the Workbench version.

Mike

Hi @mdsmith1 ,

I haven't been able to check/test this myself, but my guess is the prepared statements are the cause why this might not work.

In you Mysql resource, tick this checkbox and then run your query again:
image

This will however (temporarily) break all your other queries, so in case this is the solution, I'd suggest to create/duplicate your resource.

1 Like