DELIMITER //
CREATE PROCEDURE pbackup1(IN mContinue)
BEGIN
let mContinue = "YES";
while (mContinue = "YES") {
SELECT @CCNT1 := COUNT(Check01) FROM Invoices02 where Userno = {{ localStorage.values.Userno }} and Check01 = 1 and EXTRA1 = " ";
IF @CCNT1 > O THEN
@mInvnbr := SELECT Invnbr FROM Invoices02 WHERE Userno = {{ localStorage.values.Userno }} AND Check01 = 1 and EXTRA1 = " ";
update Invoices02 set EXTRA1 = "X" where Invnbr = @mInvnbr;
ELSE
mContinue = 'NO';
END IF;
}
END; //
DELIMITER ;
I am getting an error message about the first few lines in this program.
I have done some more work on this with MySQL Workbench and have come up with code that works in the MySQL Workbench environment as below. But it will not run in Retool. Further I get only general debug comments that are useless.
Any suggestions?
Mike
DROP PROCEDURE IF EXISTS pbackup1 ;
DELIMITER //
CREATE PROCEDURE pbackup1
BEGIN
DECLARE mContinue TINYTEXT DEFAULT " ";
DECLARE count1 INT DEFAULT 1;
DECLARE CCNT1 INT DEFAULT 1;
set mContinue = "YES";
WHILE mContinue = "YES" DO
set CCNT1 := (SELECT COUNT(Check01) FROM Invoices02 WHERE EXTRA1 = " " );
IF CCNT1 > 0 THEN
set @mInvnbr := (SELECT Invnbr FROM Invoices02 WHERE Check01 = 1 and EXTRA1 = " ");
UPDATE Invoices02 SET EXTRA1 = 'X' WHERE Invnbr = @mInvnbr;
ELSE
SET mContinue = 'NO';
END IF;
END WHILE;
END //
DELIMITER ;
Defining a procedure will be a little different when we do it via a Retool query because we don't have to worry about delimiters. In fact, trying to utilize a delimiter was throwing errors for me when doing some testing. The following worked for me, though:
Generally speaking, the primary use case for delimiters is when we're defining a procedure via the mysql command line. I hope that helps! Let me know if you run into any additional issues.
DROP PROCEDURE IF EXISTS pbackup1;
CREATE PROCEDURE pbackup1 ()
BEGIN
DECLARE mContinue TINYTEXT DEFAULT " ";
DECLARE count1 INT DEFAULT 1;
DECLARE CCNT1 INT DEFAULT 1;
SET mContinue = "YES";
WHILE mContinue = "YES" DO
SET
CCNT1 := (
SELECT
COUNT(Check01)
FROM
Invoices02
WHERE
EXTRA1 = 5
);
IF CCNT1 > 0 THEN
SET
@mInvnbr := (
SELECT
Invnbr
FROM
Invoices02
WHERE
Check01 = 1
AND EXTRA1 = 5
);
UPDATE
Invoices02
SET
EXTRA1 = 10
WHERE
Invnbr = @mInvnbr;
ELSE
SET
mContinue = 'NO';
END IF;
END
WHILE;
END;
update Invoices02 set EXTRA1 = " " ;
DROP PROCEDURE IF EXISTS pbackup1;
CREATE PROCEDURE pbackup1 ()
BEGIN
DECLARE mContinue TINYTEXT DEFAULT " ";
DECLARE count1 INT DEFAULT 1;
DECLARE CCNT1 INT DEFAULT 1;
SET mContinue = "YES";
WHILE mContinue = "YES" DO
SET
CCNT1 := (
SELECT
COUNT(Check01)
FROM
Invoices02
WHERE
Check01 = 1 and EXTRA1 = " " )
;
IF CCNT1 > 0 THEN
SET
@mInvnbr := (
SELECT
Invnbr
FROM
Invoices02
WHERE
Check01 = 1
AND EXTRA1 = " "
);
UPDATE Invoices02 SET EXTRA1 = "X" WHERE Invnbr = @mInvnbr;
ELSE
SET
mContinue = 'NO';
END IF;
END WHILE;
END;
This code does not work. I use GODADDY to watch the Invoices02 Table.
I don't think there is any result for @mInvnbr.
The console.log feature only works on JS so I am unable to use it in MySQL code and that is unfortunate.
Sorry, I should have clarified - as written, this code will define a procedure but not actually execute it. As such, you only need to run this particular query once. Whenever you want this procedure to execute, you'll need to run the query CALL pbackup1();.
Let me know if the Invoices02 table updates appropriately when you call the procedure!
This is really helpful to me. I have been struggling to get Stored Procedures to work in a number of forums for over a year and I have not been able to get to first base.